該問題糾結(jié)了我半天時間。。。查了很多資料,最后才發(fā)現(xiàn)調(diào)用writeToFile返回false的原因。在此附上解決方案:
NSData?*imageData =?UIImageJPEGRepresentation(currentImage,?1.0f);//currentImage是傳過來的UIImage
NSString *inewImageName=[self?getImagePath:imageName];//imageName為圖片名稱
BOOL?operation = [imageData?writeToFile:newImageName?options:NSAtomicWrite?error:&error];
方法:
- (NSString*)getImagePath:(NSString?*)name {
?NSArray?*path =?NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,?NSUserDomainMask,?YES);
?NSString?*docPath = [path?objectAtIndex:0];
?NSFileManager?*fileManager = [NSFileManager?defaultManager];
?NSString?*finalPath = [docPath?stringByAppendingPathComponent:name];
?// Remove the filename and create the remaining path
[fileManager?createDirectoryAtPath:[finalPath?stringByDeletingLastPathComponent]?withIntermediateDirectories:YES?attributes:nil?error:nil];//stringByDeletingLastPathComponent是關(guān)鍵
?return?finalPath;
}
希望能幫助大家解決問題哈~