[英]Zooming UIImage/CGImage
I am implementing a zooming feature in a camera app using AVFoundation
. 我正在使用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. 现在,在拍照后,我想先使用cameraZoom
值缩放/裁剪图片,然后cameraZoom
其保存到相机胶卷中。 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. 另一个问题(在OP中我应该更清楚了)是图像保持相同的分辨率,但我宁愿将其裁剪掉。
+ (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;
}
to scale/zoom: 缩放/缩放:
CGBitmapContext
创建一个CGBitmapContext
CGContextScaleCTM
) 更改上下文的转换( CGContextScaleCTM
) CGContextDrawImage
) - the rect you pass may be used to offset the origin and/or dimensions. 绘制图像( CGContextDrawImage
)-您传递的矩形可用于偏移原点和/或尺寸。 CGBitmapContextCreateImage
) 从上下文生成一个新的CGImage( CGBitmapContextCreateImage
) to crop: 种植:
CGBitmapContext
. 创建一个CGBitmapContext
。 pass NULL so the context creates is buffer for the bitmap. 传递NULL,以便上下文为位图创建缓冲区。 CGContextDrawImage
) 按原样绘制图像( CGContextDrawImage
) CGBitmapContext
for the crop (with the new dimensions), using an offset of the first context's pixel data for the buffer. 使用缓冲区的第一个上下文的像素数据的偏移量,为作物(使用新尺寸)创建一个CGBitmapContext
。 CGBitmapContextCreateImage
) 从第二个上下文生成一个新的CGImage( CGBitmapContextCreateImage
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.