可以利用这种方式实现图片UIImage翻转,本人用于自拍后,对左右相反照片的处理

来源:互联网 发布:知享科技 编辑:程序博客网 时间:2024/04/29 14:46

finishImage为需要处理的照片

在这之前需定义一下:

CGFloat DegreesToRadians(CGFloat degrees) {return degrees *M_PI / 180;};

CGFloat RadiansToDegrees(CGFloat radians) {return radians;};

  

UIImageView *imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,640, 480)];

        

        NSData *imgData =UIImagePNGRepresentation(finishImage);

       finishImage = [UIImageimageWithData:imgData];

        

       UIImage *image=finishImage;

        

        CGRect rect =CGRectMake(0,0,self.preview.frame.size.width,self.preview.frame.size.height);//创建矩形框

        UIGraphicsBeginImageContext(rect.size);//根据size大小创建一个基于位图的图形上下文

       CGContextRef currentContext =UIGraphicsGetCurrentContext();//获取当前quartz 2d绘图环境

       CGContextClipToRect( currentContext, rect);//设置当前绘图环境到矩形框

       CGContextDrawImage(currentContext, rect, image.CGImage);//绘图

        UIImage *cropped =UIGraphicsGetImageFromCurrentImageContext();//获得图片

        UIGraphicsEndImageContext();//从当前堆栈中删除quartz 2d绘图环境


       finishImage=cropped;

        imgView.image =finishImage;

//取出处理之后的照片

        finishImage = [KCCommonimageRotatedByRadians:90andImageView:imgView];



+ (UIImage *)imageRotatedByRadians:(CGFloat)radians andImageView:(UIImageView *)imageView

{

    return [KCCommonimageRotatedByDegrees:RadiansToDegrees(radians)andImageView:imageView];

}


+ (UIImage *)imageRotatedByDegrees:(CGFloat)degrees andImageView:(UIImageView *)imageView

{

    // calculate the size of the rotated view's containing box for our drawing space

   UIView *rotatedViewBox = [[UIViewalloc]initWithFrame:imageView.frame];

    CGAffineTransform t =CGAffineTransformMakeRotation(DegreesToRadians(degrees));

    rotatedViewBox.transform = t;

   CGSize rotatedSize = rotatedViewBox.frame.size;

    [rotatedViewBoxrelease];

    

    // Create the bitmap context

    UIGraphicsBeginImageContext(rotatedSize);

    CGContextRef bitmap =UIGraphicsGetCurrentContext();

    

    // Move the origin to the middle of the image so we will rotate and scale around the center.

   CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);

    

    //   // Rotate the image context

   CGContextRotateCTM(bitmap,DegreesToRadians(degrees));

    

    // Now, draw the rotated/scaled image into the context

   CGContextScaleCTM(bitmap,1.0, -1.0);

   CGContextDrawImage(bitmap,CGRectMake(-imageView.frame.size.width /2, -imageView.frame.size.height /2, imageView.frame.size.width, imageView.frame.size.height), [imageView.image CGImage]);

    

    UIImage *newImage =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

   return newImage;

    

}


0 0
原创粉丝点击