關(guān)于UIImage轉(zhuǎn)NSData (UIImagePNGRepresentation)返回為nil的情況

業(yè)務(wù)場(chǎng)景:

在自己做二維碼生成,生成以后需要傳遞圖片到后端,結(jié)果發(fā)現(xiàn)APP一直在閃退,斷點(diǎn)調(diào)試的時(shí)候,發(fā)現(xiàn)問題出現(xiàn)在了UIImage轉(zhuǎn)NSData這一步,也就是以下這兩個(gè)方法:

UIImagePNGRepresentation

UIImageJPEGRepresentation

(兩個(gè)方法的差異我就不贅述了,可以自己百度下)

實(shí)際在執(zhí)行這個(gè)方式時(shí),返回了一個(gè)nil對(duì)象,導(dǎo)致了上傳的時(shí)候,appendPartWithFileData數(shù)據(jù)為空而崩潰了;其實(shí)以前一直是這樣操作的,一直沒想到為什么會(huì)為nil

經(jīng)過參考官方文檔,發(fā)現(xiàn)了問題:
在蘋果官方的文檔里面寫著這么一句
“ return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least)”

也就是當(dāng)沒有CGImageRef的時(shí)候,會(huì)造成這個(gè)方法返回為空;然后我又回溯了一下自己生成二維碼的方法:

CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
 [qrFilter setValue:stringData forKey:@"inputMessage"];
 [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"];
    
CIImage *qrImage = qrFilter.outputImage;
//放大并繪制二維碼 (上面生成的二維碼很小,需要放大)
CGImageRef cgImage = [[CIContext contextWithOptions:nil] createCGImage:qrImage fromRect:qrImage.extent];
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
//翻轉(zhuǎn)一下圖片 不然生成的QRCode就是上下顛倒的
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, CGContextGetClipBoundingBox(context), cgImage);
UIImage *codeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
    
CGImageRelease(cgImage);
    
//繪制顏色
CIFilter *colorFilter = [CIFilter filterWithName:@"CIFalseColor"
                                       keysAndValues:
                             @"inputImage",[CIImage imageWithCGImage:codeImage.CGImage],
                             @"inputColor0",[CIColor colorWithCGColor:frontColor == nil ? [UIColor clearColor].CGColor: frontColor.CGColor],
                             @"inputColor1",[CIColor colorWithCGColor: backColor == nil ? [UIColor blackColor].CGColor : backColor.CGColor],
                             nil];
    
UIImage * colorCodeImage = [UIImage imageWithCIImage:colorFilter.outputImage];

其實(shí)自己用到的,就是CIImage,當(dāng)執(zhí)行方法生成的圖片,去打印 colorCodeImage的CGImage

NSLog(@"%@", colorCodeImage.CGImage)

執(zhí)行結(jié)果是nil

以此,找到了問題的所在,如果這樣的話,那就重新生成一下圖片就行了
直接貼上代碼,需要的研究一下

- (UIImage *)scaleImage:(UIImage *)image{
    //確定壓縮后的size
    CGFloat scaleWidth = image.size.width;
    CGFloat scaleHeight = image.size.height;
    CGSize scaleSize = CGSizeMake(scaleWidth, scaleHeight);
    //開啟圖形上下文
    UIGraphicsBeginImageContext(scaleSize);
    //繪制圖片
    [image drawInRect:CGRectMake(0, 0, scaleWidth, scaleHeight)];
    //從圖形上下文獲取圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //關(guān)閉圖形上下文
    UIGraphicsEndImageContext();
    return newImage;
}

當(dāng)拿到現(xiàn)在的newImage的時(shí)候,發(fā)現(xiàn)正常能夠執(zhí)行相應(yīng)的轉(zhuǎn)換NSData的方法了;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容