Objective-C iOS裁剪图像使用缩放?

时间:2012-12-09 02:47:29

标签: ios zoom crop

我正在尝试使用此代码来允许用户在我的应用中缩放和裁剪图像: https://github.com/iosdeveloper/ImageCropper/tree/master/Classes/ImageCropper

代码段

float zoomScale = 1.0 / [scrollView zoomScale];

CGRect rect;
rect.origin.x = [scrollView contentOffset].x * zoomScale;
rect.origin.y = [scrollView contentOffset].y * zoomScale;
rect.size.width = [scrollView bounds].size.width * zoomScale;
rect.size.height = [scrollView bounds].size.height * zoomScale;

CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);

UIImage *cropped = [UIImage imageWithCGImage:cr];

CGImageRelease(cr);

[delegate imageCropper:self didFinishCroppingWithImage:cropped];

但它没有考虑旋转的图像(我猜它是iPhone图像上的某种元数据)。我不想让用户绘制一个矩形来裁剪,因为我更喜欢这个界面。

有人可以告诉我如何让它不旋转我的图像吗?

我在iOS 5& 6如果有帮助的话。

[编辑]此外,这在游戏中心应用程序中很好地完成了上传您的个人资料图片。有人知道我在哪里可以找到代码吗?

1 个答案:

答案 0 :(得分:3)

我在这里找到了一个帮助我做我想要的答案 iOS: Cropping a still image grabbed from a UIImagePickerController camera with overlay

感兴趣的代码段

float zoomScale = 1.0 / [scrollView zoomScale];

CGRect rect;
NSLog(@"contentOffset is :%f,%f",[scrollView contentOffset].x,[scrollView contentOffset].y);
rect.origin.x = fabsf([scrollView contentOffset].x * zoomScale);
rect.origin.y = fabsf([scrollView contentOffset].y * zoomScale);
rect.size.width = fabsf([scrollView bounds].size.width * zoomScale);
rect.size.height = fabsf([scrollView bounds].size.height * zoomScale);

UIGraphicsBeginImageContextWithOptions( CGSizeMake( rect.size.width,
                                                   rect.size.height),
                                       NO,
                                       0.);
[[imageView image] drawAtPoint:CGPointMake( -rect.origin.x, -rect.origin.y)
            blendMode:kCGBlendModeCopy
                alpha:1.];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();