-2

I am attempting to delete or overwrite an image within a specific directory. I grab all my images in an array and place in a table view. When an image is clicked photo roll is opened and the user chooses an image. when the imagepicker controller is being released I call out my methods to try and delete the original image and save the new image to the specific filepath. I receive a odd error when deleting the image and not sure how I can give a better log to save the image but it is not saving either. I would appreciate an input!

    - (void)imagePickerController:(UIImagePickerController *)picker   
didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"image picker did finish");




widgetImg.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

change.enabled = YES;

[self saveImage:widgetImg.image :ImgName];
NSString *path = [NSString 
stringWithFormat:@"/Library/Themes/%@/iSetUp/UserPhotos/%@",selectedThemeName,ImgName]; 


NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL fileExists = [fileManager fileExistsAtPath:path];
NSLog(@"Path to file: %@", path);        
NSLog(@"File exists: %d", fileExists);
NSLog(@"Is deletable file at path: %d", [fileManager isDeletableFileAtPath:path]);
if (fileExists) 
{
    BOOL success = [fileManager removeItemAtPath:path error:&error];
    if (!success) NSLog(@"Error: %@", [error localizedDescription]);
}


[self dismissModalViewControllerAnimated:YES]; 



}



- (void)saveImage:(UIImage*)image:(NSString*)imageName {  
//convert image into .png format.  
NSData *imageData = UIImagePNGRepresentation(image);  
NSFileManager *fileManager = [NSFileManager defaultManager];   
NSString *IMGPath = [NSString 
stringWithFormat:@"/Library/Themes/%@/iSetUp/UserPhotos/",selectedThemeName]; 


[fileManager createFileAtPath:IMGPath contents:imageData attributes:nil];  
NSLog(@"image saved"); 

} 


2012-03-29 16:05:05.907 iSetUp[1258:707] image picker did finish
2012-03-29 16:05:09.046 iSetUp[1258:707] image saved
2012-03-29 16:05:09.047 iSetUp[1258:707] Path to file:   
/Library/Themes/Modernistic/iSetUp/UserPhotos/IMG_0183.JPG
2012-03-29 16:05:09.048 iSetUp[1258:707] File exists: 1
2012-03-29 16:05:09.049 iSetUp[1258:707] Is deletable file at path: 0
2012-03-29 16:05:09.053 iSetUp[1258:707] Error: The operation couldn’t be completed.   
(Cocoa error 513.)
FreeAppl3
  • 858
  • 1
  • 15
  • 32

2 Answers2

1

You cannot directly access folders like that through the iOS SDK. You must save and read files using the system functions that return the proper path:

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
NSString *pathToMenuLoopFile = [rootPath stringByAppendingPathComponent:@"Menu Loop.mp3"];

If you are looking to load files that you include in your Xcode project, please take a look at this post: Loading data files in iPhone project

Community
  • 1
  • 1
rosslebeau
  • 1,273
  • 6
  • 6
  • Wish I could help, I've never written apps for jailbroken phones or anything. I would also be interested to know how to do what you are asking. Maybe you should specify that in the question so people with that knowledge will find it? – rosslebeau Mar 29 '12 at 20:46
  • You can get the idea from this post http://stackoverflow.com/questions/3847239/iphone-run-app-as-root – FreeAppl3 Apr 22 '12 at 20:30
-1

The file is not deletable, either due to an ownership or permissions issue. See the discussion of isDeletableFileAtPath in

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

DRVic
  • 2,413
  • 1
  • 13
  • 20