从中心向外裁剪 UIImage?

发布于 2024-08-14 17:47:18 字数 1531 浏览 4 评论 0原文

我正在制作一个包含数字变焦的相机应用程序。我有一个滑块 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-08-21 17:47:18
CGFloat newWidth = image.size.width/zoomSlider.value;
CGFloat newHeight = image.size.height/zoomSlider.value;

CGRect clippedRect = CGRectMake((image.size.width-newWidth)/2, (image.size.height-newHeight)/2, newWidth, newHeight);

删除它以不旋转图像:

CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,RADIANS(90.0));

zoomPhoto.transform = rotateTransform;
CGFloat newWidth = image.size.width/zoomSlider.value;
CGFloat newHeight = image.size.height/zoomSlider.value;

CGRect clippedRect = CGRectMake((image.size.width-newWidth)/2, (image.size.height-newHeight)/2, newWidth, newHeight);

Remove this to not rotate the image:

CGAffineTransform rotateTransform = CGAffineTransformRotate(CGAffineTransformIdentity,RADIANS(90.0));

zoomPhoto.transform = rotateTransform;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文