从中心向外裁剪 UIImage?
我正在制作一个包含数字变焦的相机应用程序。我有一个滑块 (zoomSlider),其最小值为 1,最大值为 4。当用户点击相机按钮时,它会拍摄一张照片,然后我对其进行裁剪以进行缩放。我有两个问题:
如何裁剪图像的中间部分? (例如,2 倍缩放,矩形将以 600x800 的尺寸居中(对于 iPhone 2G/3G))
当我这样做时,它会旋转图像。我通过旋转它所在的 UIImageView 来弥补它,但这会导致纵向图片变成横向图片,反之亦然。
这是我的代码:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if ([mediaType isEqualToString:@"public.image"]){
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGRect clippedRect = CGRectMake(600, 450, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);
UIImage *cropped = [self imageByCropping:image toRect:clippedRect];
CGRect croppedImageSize = CGRectMake(0, 0, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);
[cropped drawInRect:croppedImageSize];
zoomPhoto.frame = croppedImageSize;
zoomPhoto.image = cropped;
CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
RADIANS(90.0));
zoomPhoto.transform = rotateTransform;
}
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
I am making a camera app which includes digital zoom. I have a slider (zoomSlider) that has a minimum value of 1 and a maximum value of 4. When the user taps the camera button, it takes a picture, and then I crop it for the zooming. I have two problems:
How do I crop the exact middle of the image? (eg. 2x zoom, Rect would be centered with dimensions of 600x800 (for iPhone 2G/3G))
When I do this, it rotates the image. I make up for it by rotating the UIImageView it's in, but this causes a portrait picture to become landscape and vice versa.
Here is my code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if ([mediaType isEqualToString:@"public.image"]){
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGRect clippedRect = CGRectMake(600, 450, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);
UIImage *cropped = [self imageByCropping:image toRect:clippedRect];
CGRect croppedImageSize = CGRectMake(0, 0, image.size.width/zoomSlider.value, image.size.height/zoomSlider.value);
[cropped drawInRect:croppedImageSize];
zoomPhoto.frame = croppedImageSize;
zoomPhoto.image = cropped;
CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,
RADIANS(90.0));
zoomPhoto.transform = rotateTransform;
}
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除它以不旋转图像:
Remove this to not rotate the image: