【问题标题】:How to know if a UIImage is representable in PNG or JPG?如何知道 UIImage 是否可以用 PNG 或 JPG 表示?
【发布时间】:2013-06-26 09:43:45
【问题描述】:

我从UIImagePickerController 得到一个UIImage,并使用this site 中的代码调整图像大小

- (UIImage *)resizedImage:(CGSize)newSize
                transform:(CGAffineTransform)transform
           drawTransposed:(BOOL)transpose
     interpolationQuality:(CGInterpolationQuality)quality {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
    CGImageRef imageRef = self.CGImage;

    // Build a context that's the same dimensions as the new size
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                CGImageGetBitmapInfo(imageRef));

    // Rotate and/or flip the image if required by its orientation
    CGContextConcatCTM(bitmap, transform);

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(bitmap, quality);

    // Draw into the context; this scales the image
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;
}

UIImagePNGRepresentation() 无法在调整大小的图像上返回 NSData,但 UIImageJPEGRepresentation() 成功。

我们如何知道UIImage 是否可以以 PNG 或 JPEG 格式呈现?上述代码中遗漏了什么使调整后的图像无法用 PNG 表示?

根据苹果文档:“如果图像没有数据或底层 CGImageRef 包含不受支持的位图格式的数据,此函数可能返回 nil。”

PNG 演示文稿支持哪些位图格式?如何制作 UIImage 支持 PNG 的格式?

【问题讨论】:

  • 失败的图像的颜色/像素格式是什么?
  • 我不是在谈论]图像的特定颜色分量或像素。我的意思是 UIImagePNGRepresentation(resultimage) 返回 nil
  • 我在询问您的 CGContextRef 的颜色/像素格式。这是我能想到的 UIPNGRepresentation 失败的唯一原因(不支持的颜色格式/像素格式)。
  • @borrrden 你是对的,图像由 CGBitmapContextCreate 和 kCGImageAlphaNoneSkipFirst 创建。谢谢

标签: iphone ios objective-c uiimage


【解决方案1】:

这是一个错误,在代码的另一部分中,图像被以下内容重新缩放

CGContextRef context = CGBitmapContextCreate(NULL,
                                         size.width,
                                         size.height,
                                         8,
                                         0,
                                         CGImageGetColorSpace(source),
                                         kCGImageAlphaNoneSkipFirst);

将 kCGImageAlphaNoneSkipFirst 更改为 CGImageGetBitmapInfo(source) 解决了问题

【讨论】:

    【解决方案2】:

    转到以下链接...

    How to check if downloaded PNG image is corrupt?

    它可以帮助你...

    让我知道它是否有效...

    编码愉快!!!!

    【讨论】:

    • 从长远来看,仅链接的答案并不是很有帮助。如果您只是链接到一个 SO 答案,最好将问题标记为重复。
    猜你喜欢
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2017-06-11
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多