缩放 UIImage/CGImage
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
缩放/缩放:
CGBitmapContext
CGContextScaleCTM
)CGContextDrawImage
) - 您传递的矩形可能会被使用偏移原点和/或尺寸。CGBitmapContextCreateImage
) 生成一个新的 CGImage进行裁剪:
CGBitmapContext
。传递 NULL,以便上下文为位图创建缓冲区。CGContextDrawImage
)CGBitmapContext
(具有新尺寸)。CGBitmapContextCreateImage
)to scale/zoom:
CGBitmapContext
CGContextScaleCTM
)CGContextDrawImage
) - the rect you pass may be used to offset the origin and/or dimensions.CGBitmapContextCreateImage
)to crop:
CGBitmapContext
. pass NULL so the context creates is buffer for the bitmap.CGContextDrawImage
)CGBitmapContext
for the crop (with the new dimensions), using an offset of the first context's pixel data for the buffer.CGBitmapContextCreateImage
)