缩放 UIImage/CGImage

发布于 2024-12-28 10:54:11 字数 1046 浏览 0 评论 0原文

我正在使用 AVFoundation 在相机应用程序中实现缩放功能。我像这样缩放预览视图:

[videoPreviewView setTransform:CGAffineTransformMakeScale(cameraZoom, cameraZoom)];

现在,在拍照后,我想在将图片保存到相机胶卷之前使用 cameraZoom 值缩放/裁剪图片。我应该如何最好地做到这一点?

编辑:使用贾斯汀的答案:

CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], imageRect);

CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef));

CGContextScaleCTM(bitmapContext, scale, scale);
CGContextDrawImage(bitmapContext, imageRect, imageRef);

CGImageRef zoomedCGImage = CGBitmapContextCreateImage(bitmapContext);

UIImage* zoomedImage = [[UIImage alloc] initWithCGImage:imageRef];

它正在缩放图像,但它没有占据图像的中心,而是似乎占据了右上角的区域。 (我并不积极)。

另一个问题(我应该在OP中更清楚)是图像保持相同的分辨率,但我宁愿将其裁剪下来。

I am implementing a zooming feature in a camera app using AVFoundation. I am scaling my preview view like this:

[videoPreviewView setTransform:CGAffineTransformMakeScale(cameraZoom, cameraZoom)];

Now, after I take a picture, I would like to zoom/crop the picture with the cameraZoom value before I save it to the camera roll. How best should I do this?

Edit: Using Justin's answer:

CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);

CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], imageRect);

CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), CGImageGetColorSpace(imageRef), CGImageGetBitmapInfo(imageRef));

CGContextScaleCTM(bitmapContext, scale, scale);
CGContextDrawImage(bitmapContext, imageRect, imageRef);

CGImageRef zoomedCGImage = CGBitmapContextCreateImage(bitmapContext);

UIImage* zoomedImage = [[UIImage alloc] initWithCGImage:imageRef];

It is zooming the image, but it is not taking the center of it, but rather seems to be taking the top right area. (I'm not positive).

The other problem, (I should have been clearer in the OP), is that the image remains the same resolution, but I would rather just crop it down.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

笑咖 2025-01-04 10:54:11
+ (UIImage*)croppedImageWithImage:(UIImage *)image zoom:(CGFloat)zoom
{
    CGFloat zoomReciprocal = 1.0f / zoom;

    CGPoint offset = CGPointMake(image.size.width * ((1.0f - zoomReciprocal) / 2.0f), image.size.height * ((1.0f - zoomReciprocal) / 2.0f));
    CGRect croppedRect = CGRectMake(offset.x, offset.y, image.size.width * zoomReciprocal, image.size.height * zoomReciprocal);

    CGImageRef croppedImageRef = CGImageCreateWithImageInRect([image CGImage], croppedRect);

    UIImage* croppedImage = [[UIImage alloc] initWithCGImage:croppedImageRef scale:[image scale] orientation:[image imageOrientation]];

    CGImageRelease(croppedImageRef);

    return croppedImage;
}
+ (UIImage*)croppedImageWithImage:(UIImage *)image zoom:(CGFloat)zoom
{
    CGFloat zoomReciprocal = 1.0f / zoom;

    CGPoint offset = CGPointMake(image.size.width * ((1.0f - zoomReciprocal) / 2.0f), image.size.height * ((1.0f - zoomReciprocal) / 2.0f));
    CGRect croppedRect = CGRectMake(offset.x, offset.y, image.size.width * zoomReciprocal, image.size.height * zoomReciprocal);

    CGImageRef croppedImageRef = CGImageCreateWithImageInRect([image CGImage], croppedRect);

    UIImage* croppedImage = [[UIImage alloc] initWithCGImage:croppedImageRef scale:[image scale] orientation:[image imageOrientation]];

    CGImageRelease(croppedImageRef);

    return croppedImage;
}
浅黛梨妆こ 2025-01-04 10:54:11

缩放/缩放:

  • 创建一个CGBitmapContext
  • 改变上下文的变换(CGContextScaleCTM
  • 绘制图像(CGContextDrawImage) - 您传递的矩形可能会被使用偏移原点和/或尺寸。
  • 从上下文 (CGBitmapContextCreateImage) 生成一个新的 CGImage

进行裁剪:

  • 创建一个 CGBitmapContext。传递 NULL,以便上下文为位图创建缓冲区。
  • 按原样绘制图像 (CGContextDrawImage)
  • ,使用缓冲区的第一个上下文像素数据的偏移量为裁剪创建一个 CGBitmapContext(具有新尺寸)。
  • 从第二个上下文生成一个新的 CGImage (CGBitmapContextCreateImage)

to scale/zoom:

  • create a CGBitmapContext
  • alter the context's transform (CGContextScaleCTM)
  • draw the image (CGContextDrawImage) - the rect you pass may be used to offset the origin and/or dimensions.
  • generate a new CGImage from the context (CGBitmapContextCreateImage)

to crop:

  • create a CGBitmapContext. pass NULL so the context creates is buffer for the bitmap.
  • draw the image as-is (CGContextDrawImage)
  • create a CGBitmapContext for the crop (with the new dimensions), using an offset of the first context's pixel data for the buffer.
  • generate a new CGImage from the second context (CGBitmapContextCreateImage)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文