Objective-C iOS Crop Image Using Zoom?

﹥>﹥吖頭↗ 提交于 2019-12-04 14:25:37

问题


I am trying to use this code to allow users to zoom and crop their images in my app: https://github.com/iosdeveloper/ImageCropper/tree/master/Classes/ImageCropper

code snippet

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];

But it does not take into account images being rotated (I guess it's some sort of metadata on the iPhone images). I do not want to have users draw a rectangle to crop as I much prefer this interface.

Can someone tell me how to make it not rotate my images?

I am building in iOS 5 & 6 if that helps.

[EDIT] Also, this is done nicely in the Game Center application for uploading your profile picture. Anyone know where I can find the code for that?


回答1:


I found an answer here that helped me do what I wanted iOS: Cropping a still image grabbed from a UIImagePickerController camera with overlay

Code Snippet for those interested

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();


来源:https://stackoverflow.com/questions/13784182/objective-c-ios-crop-image-using-zoom

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!