【问题标题】:Scale incorrect with resize UIImage and export to jpeg调整 UIImage 大小并导出为 jpeg 时缩放不正确
【发布时间】:2018-07-31 00:53:50
【问题描述】:

您好,我有以下函数来保存具有比例因子的 jpg 图像

- (UIImage*) pixelBufferToUIImage:(CVPixelBufferRef) pixelBuffer
{
    CIImage *ciImage = [CIImage imageWithCVPixelBuffer:pixelBuffer];

    CIContext *temporaryContext = [CIContext contextWithOptions:nil];
    CGImageRef videoImage = [temporaryContext
                             createCGImage:ciImage
                             fromRect:CGRectMake(0, 0,
                                                 CVPixelBufferGetWidth(pixelBuffer),
                                                 CVPixelBufferGetHeight(pixelBuffer))];
    UIImage *uiImage = [UIImage imageWithCGImage:videoImage scale:(1/scale_factor) orientation:UIImageOrientationUp];
    CGImageRelease(videoImage);
    return uiImage;
}

我这样称呼这个方法,

NSData* image = UIImageJPEGRepresentation([self pixelBufferToUIImage:frame.capturedImage], 0.8);
[image writeToFile:[self getFilePathWith:folderName and:@"image.jpg"] atomically:NO];

如果我使用原始分辨率 scale_factor = 1.0f 调用保存,我会得到 1280x720 图像

但是,我想将图像大小缩小一半。我尝试设置scale_factor = 0.5f and 2.0f两者都不起作用,我得到的jpg大小为1280x720

谁能在这里指出我的错误?

【问题讨论】:

    标签: ios objective-c image-processing scale


    【解决方案1】:

    文档说scale 将改变 size 属性报告的内容。它不会改变图像的像素数据。 UIImage 的内部表示仍然具有相同的尺寸。

    scale
    解释图像数据时使用的比例因子。 将比例因子指定为 1.0 会生成大小为 匹配图像的基于像素的尺寸。应用不同的 比例因子改变图像的大小,由大小报告 属性。

    要缩放图像,您需要自己进行缩放。

    Have a look here how to do it

    所以你可以这样做。将方法放在类中的链接中,然后在将图像转换为 JPEG 之前添加缩放调用。

    CGFloat newScale = 0.5;
    UIImage *uiImage = [self pixelBufferToUIImage:frame.capturedImage];
    uiImage = [YourClass imageWithImage:uiImage scaledToSize:CGSizeMake(uiImage.size.width * newScale, uiImage.size.height * newScale)];
    NSData* image = UIImageJPEGRepresentation(uiImage, 0.8);
    [image writeToFile:[self getFilePathWith:folderName and:@"image.jpg"] atomically:NO];
    

    【讨论】:

    • 知道了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 2016-12-18
    • 1970-01-01
    • 2015-11-20
    • 2017-02-06
    相关资源
    最近更新 更多