如何知道UIImage是否可以用PNG或JPG表示



我从UIImagePickerController得到了一个UIImage,并使用该站点的代码来调整图像的大小

- (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支持的格式?

这是一个错误,在代码的另一部分中,使用以下重新缩放了图像

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

将kCGImageAlphaNoneSkipFirst更改为CGImageGetBitmapInfo(源)修复了的问题

转到以下链接。。。

如何检查下载的PNG图像是否已损坏?

它可能会帮助你。。。

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

快乐编码!!!!

最新更新