簡體   English   中英

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

[英]How to know if a UIImage is representable in PNG or 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表示?

根據Apple的文檔:“如果圖像沒有數據或基礎CGImageRef包含不受支持的位圖格式的數據,則此函數可能返回nil。”

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

這是一個錯誤,在代碼的另一部分中,圖像使用以下內容重新縮放

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

將kCGImageAlphaNoneSkipFirst更改為CGImageGetBitmapInfo(source)可解決此問題

轉到以下鏈接...

如何檢查下載的PNG圖像是否損壞?

它可以幫助您...

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

編碼愉快!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM