2011-05-25 916 views

回答

14

這應該工作:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *docs = [paths objectAtIndex:0]; 
NSString* path = [docs stringByAppendingFormat:@"/image1.jpg"]; 

NSData* imageData = [NSData dataWithData:UIImageJPEGRepresentation(imageView.image, .8)]; 
NSError *writeError = nil; 

if(![imageData writeToFile:path options:NSDataWritingAtomic error:&writeError]) { 
    NSLog(@"%@: Error saving image: %@", [self class], [writeError localizedDescription]); 
} 
+1

這是不正確的檢查在檢查對'writeToFile:'調用的返回值之前的錯誤對象。如果'BOOL'返回值爲'NO',則只能保證有效的對象;如果它是'YES',那麼錯誤不會保證是'nil'。 – 2011-05-25 07:47:50

+0

感謝您的回覆。 – 2011-05-25 08:47:06

2

小心。此行有一個錯誤:

UIImageJPEGRepresentation(imageView.image, 80) 

如果你看一下頭,壓縮質量應該是0(最有損)和1(最低壓縮)之間的浮動

UIKIT_EXTERN NSData *UIImageJPEGRepresentation(UIImage *image, CGFloat compressionQuality); // return image as JPEG. May return nil if image has no CGImageRef or invalid bitmap format. compression is 0(most)..1(least) 
相關問題