我正在使用AVFoundation在相机应用程序中实现缩放功能。我像这样缩放我的预览视图:
[videoPreviewView setTransform:CGAffineTransformMakeScale(cameraZoom, cameraZoom)];现在,在我拍摄一张照片后,我想在将其保存到相机胶卷之前,使用cameraZoom值缩放/裁剪照片。我应该怎样做才是最好的呢?
编辑:使用Justin的答案:
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];它正在缩放图像,但它并没有占据图像的中心,而是似乎占据了右上角的区域。(我不确定)。
另一个问题(我在操作中应该更清楚)是图像的分辨率保持不变,但我宁愿直接将其裁剪下来。
发布于 2012-01-23 13:21:10
+ (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;
}发布于 2012-01-22 14:20:16
要缩放,请执行以下操作:
create a CGBitmapContext
CGContextScaleCTM)
CGContextDrawImage) -您传递的rect可用于偏移原点和/或从上下文转换中创建新的CGImage要裁剪,请执行以下操作:
CGBitmapContext。传递NULL,以便上下文为位图创建缓冲区。使用第一个上下文的像素数据的偏移量为来自第二个上下文(CGBitmapContextCreateImage)的(CGContextDrawImage)
CGBitmapContext绘制图像按原样绘制裁剪(具有新尺寸)的CGImagehttps://stackoverflow.com/questions/8959293
复制相似问题