【发布时间】: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