我从UIImage
获得UIImagePickerController
,并使用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()
成功。
我们如何知道PNG或JPEG中是否有UIImage
?在上面的代码中遗漏了什么使调整大小的图像无法用PNG表示?
根据apple文档:“如果图像没有数据或者底层的CGImageRef包含不支持的位图格式的数据,则此函数可能返回nil。”
PNG演示支持哪种位图格式?如何制作UIImage PNG支持的格式?
答案 0 :(得分:0)
这是一个错误,在代码的另一部分中,图像使用以下
重新调整CGContextRef context = CGBitmapContextCreate(NULL,
size.width,
size.height,
8,
0,
CGImageGetColorSpace(source),
kCGImageAlphaNoneSkipFirst);
将kCGImageAlphaNoneSkipFirst更改为CGImageGetBitmapInfo(source)修复了问题
答案 1 :(得分:-1)