2012-10-26 21 views
0

我想附加從UIGraphicsBeginImageContext呈現的圖像。作爲測試,我也將圖像添加到相冊中。它在模擬器上都能正常工作,但在設備上正確的圖像會被添加到相冊中,但不會正確顯示在電子郵件附件中。我認爲這是因爲它是一個很大的圖像,並且在iPhone 3gs上需要一些時間。這意味着我必須檢查它是否完成了渲染圖像。有沒有辦法呢?這裏是我的代碼:UIGraphicsBeginImageContext,從應用程序的電子郵件圖像

UIGraphicsBeginImageContext(backGround.layer.frame.size); 
[backGround.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); 
UIGraphicsEndImageContext(); 

MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init]; 
mailer.mailComposeDelegate = self; 
NSData *imgData = UIImagePNGRepresentation(image); 
[mailer addAttachmentData:imgData mimeType:@"image/png" fileName:@"myfilename"]; 

我在想,也許它不能完全與圖像結束,它仍然被破壞的數據,當我提出它的PNG表示。我能否以某種方式檢查UIImage是否「完成」?

回答

1

嘗試執行完成方法並檢查它。一個例子是,

UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL); 

- (void)image:(UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *) contextInfo { 
     NSLog(@"SAVE IMAGE COMPLETE"); 
     if(error != nil) { 
      NSLog(@"ERROR SAVING:%@", [error localizedDescription]); 
     } 

    } 

根據您可以調試的錯誤信息的錯誤消息。查詢UIImageWriteToSavedPhotosAlbum documentation瞭解更多詳情。

+0

其實,我不想保存到圖書館,我只是這樣做,以檢查圖像是否損壞。但它看起來不錯在photoalbum .. – Martol1ni

+0

但你能檢查什麼是保存到設備中的相冊顯示的錯誤信息?可能應該給你一個完整的圖片。 – iDev

+0

的確如此。我沒有收到任何錯誤,它也將圖像正確保存到相冊中。問題是,當我嘗試將它附加到電子郵件時,我認爲圖像未完成。有沒有辦法檢查圖像是否完成渲染? – Martol1ni

相關問題