????????????????ios oc writeToFile將圖片保存到本地一直失敗
?????? // 圖片名稱
? ? ? ? NSMutableString *imageName = [NSMutableString stringWithString:[[NSDate date] format:@"yyyyMMddHHmmss_SSS"]];
? ? ? ? [imageNameappendFormat:@"%d.jpg", (arc4random() %999+1)];
? ? ? ? [imageNameinsertString:@"ios_image_"atIndex:0];
? ? ? ? // 圖片存儲路徑
? ? ? ? NSString *targetPath = [TaskAttachmentPath stringByAppendingPathComponent:imageName];
? ? ? ? /*必須加上這句
? ? ? ? NSFileManager *fileManager = [NSFileManager defaultManager];
? ? ? ? [fileManagercreateDirectoryAtPath:[targetPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
**/
? ? ? ? NSData*data =UIImageJPEGRepresentation(imageArray[i], compressionQuality);
? ? ? ? if([datawriteToFile:targetPathatomically:YES]) {
? ? ? ? ? ? [MBProgressHUD showSuccess:@"圖片保存成功"];}
? //創(chuàng)建文件夾?
? @param path 要創(chuàng)建文件夾的路徑
? @param createIntermediates 是否創(chuàng)建中間文件夾 經(jīng)過實驗發(fā)現(xiàn) 如 @"/Users/plyn/Desktop/實驗4/shiyan" 路徑中/實驗4/shiyan本來是不存在的 如果這個參數(shù)是YES則可以成功地創(chuàng)建這個路徑地文件夾;如果傳NO則無法創(chuàng)建
? @param attribute 創(chuàng)建文件夾屬性 ?傳nil地話系統(tǒng)會自動創(chuàng)建
? @param error 錯誤信息
? - (BOOL)createDirectoryAtPath:(NSString *)path withIntermediateDirectories:(BOOL)createIntermediates attributes:(NSDictionary *)attributes error:(NSError **)error ;
總結(jié):由于未創(chuàng)建中間文件夾問題??