사용자가 UIImagePickerController
과 함께 찍은 사진을 사용자의 문서 디렉토리에 저장합니다. 응용 프로그램이 서버에 이미지를 성공적으로 업로드하면 로컬로 저장된 이미지를 삭제하려고합니다.목표 C - writeToFile은 성공했지만 removeFile은 동일한 파일에 대해 오류를 반환합니다.
나는이 같은 문서 디렉토리에 이미지를 저장 :
/*delete image stored in documents directory from collecting w/o internet connection*/
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:collectedLeaf.localImageFilePath];
NSError *error;
BOOL successDeleteLocalImage = [fileManager removeItemAtPath:filePath error:&error];
if(successDeleteLocalImage){
NSLog(@"SUCCESS DELETE IMAGE NO ERROR");
collectedLeaf.localImageFilePath = nil;
}else{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
성공적인 쓰기/문서 디렉토리의 사진 인쇄 successfully write to file path /var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png
을 저장
/*Save image to documents directory, as opposed to core data for better memory management*/
NSString *myUniqueName = [NSString stringWithFormat:@"%lu.png", (unsigned long)([[NSDate date] timeIntervalSince1970]*10.0)];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:myUniqueName]; //Add the file name
BOOL successWriteLocalImage = [imageToUpload writeToFile:filePath options:NSDataWritingAtomic error:&error];
if(successWriteLocalImage){
//NSLog(@"no error success writing to file");
NSLog(@"successfully write to file path %@", filePath);
}else{
NSLog(@"Unresolved error writing to file %@, %@", [error localizedDescription], [error userInfo]);
}
/*Save file path of leaf image to core data here*/
collectedLeaf.localImageFilePath = filePath;
[context save:&error];
그래서 같이 같은 파일을 삭제합니다.
파일 로그
Unresolved error Error Domain=NSCocoaErrorDomain Code=4 "“15130563374.png” couldn’t be removed." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/var/mobile/Containers/Data/Application/EA08B79A-7484-4568-82CE-079B4055CDA7/Documents/15130563374.png, NSUserStringVariant=(
), NSUnderlyingError=0x1c4a4b4c0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}, {
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=2 \"No such file or directory\"";
내가 파일을 제거 할 수없는 이유를 잘 모르겠어요 제거의 오류 - 또는 발견되지 반환을 - 때 하나 개의 I로서의 똑같은 파일 경로 에 쓰기. 당신이 코드 을주는 경로에 존재하는지 여부는 파일을 확인 sholud
편집 답변 읽기. 핵심 데이터에 절대로 절대 파일 경로를 저장하면 안됩니다. 응용 프로그램 샌드 박스 자체의 절대 경로가 응용 프로그램 종료 및 실행 사이에 변경되므로 문서 시작의 경로 사이에 문서 디렉토리의 파일 경로가 변경됩니다. –