-1

I am facing one problem. I have a directory named as Album under my Supporting Files group. In this directory I have some images. In my image editing app I want to save my edited images in my album directory. I tried to save the image in my album directory by using the following code

NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSString *albumPath = [bundleRoot stringByAppendingString:@"/Album"];
NSString *savedImagePath = [bundleRoot stringByAppendingPathComponent: @"newImage11.jpeg" ]; UIGraphicsBeginImageContext(drawImage.image.size); 
CGContextRef ctx = UIGraphicsGetCurrentContext();
 [drawImage.layer renderInContext:ctx]; 
UIImage *editedImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *editedImageData = UIImageJPEGRepresentation(editedImage, 1.0); UIGraphicsEndImageContext(); 
[editedImageData writeToFile:savedImagePath atomically:YES];

Here drawImage is the IBOutlet of UIImageView which holds the image.This code saved a file newImage11.jpeg in my album directory which has a size also. but when I retrieve the image from album directory it gives me a blank image. I used the following code to retrieve the image

NSString *img = [onlyJPGs objectAtIndex:currentIndex+numberToAdd]; 
UIImage *tempImage = [UIImage imageNamed:img]; 
imageButton.tag = img; 
[imageButton setImage:tempImage forState:UIControlStateNormal]; 
[imageButton addTarget:nil action:@selector(goToEdit:) forControlEvents:UIControlEventTouchUpInside]; 
currentIndex+=numberToAdd;

onlyJPGs is an array which holdes the name of all images contained in the Album directory. I am sure that my app is saving the image in Album directory because when I list the contents of Album directory in ImageEditing.app by using terminal it shows me the newImage11.jpeg file. Any kind of help would be appreciable.

Sergio
  • 28,539
  • 11
  • 85
  • 132
Naveen Chauhan
  • 2,026
  • 8
  • 33
  • 40

2 Answers2

0

First off you can't write to the bundlePath, the hole application bundle is read only.

Second are you sure you have created a directory in the resource group of you project and not a group (group folder icon are yellow and folders are blue).

The yellow folder which are groups are omitted when building your app, all files in this folder are places in the root of you application bundle.

You can write files to the document directory:

NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

These directory will automatically be synced with iCould and according to Apple should only contain user generated files. Any files that you app can recreate or can download again should go in the caches directory:

NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
  • Yes I created a directory in resource group. It colors blue in Xcode window. – Naveen Chauhan Dec 06 '11 at 08:57
  • but i want to retrieve and save images in a single directory (Album). If bundlePath is readonly then how can i save images in Album directory which is inside my application folder? – Naveen Chauhan Dec 06 '11 at 09:00
  • You can't! You will need to save the image in the document directory. – rckoenes Dec 06 '11 at 09:02
  • The bundle is create on compile time, you can add images there when you compile your app. But after that it is readonly. – rckoenes Dec 06 '11 at 09:09
  • Can i put some data in the document directory during compile time? – Naveen Chauhan Dec 06 '11 at 09:42
  • But when i saved the image in the album directory and list the contents of album directory in the .app folder using terminal then it list the new created image name having some size. But you are saying that it is readonly. Then can you please explain me that how there is a new created image in the .app bundle. – Naveen Chauhan Dec 06 '11 at 09:48
  • First you can not place any files in the document directory at compile time since it not there but created when the app is installed. About the second question, it will work in the simulator and on jailbroke phone. But on a normal iOS device there is no way to write to the application bundle. – rckoenes Dec 06 '11 at 09:58
  • Can i move the images from bundle to document directory? – Naveen Chauhan Dec 06 '11 at 10:01
  • Yes of course, when you app start just check if the images are in the document directory if not copy them there. – rckoenes Dec 06 '11 at 10:02
  • Can you please give me a sample code by which i can do this kind of stuff? – Naveen Chauhan Dec 06 '11 at 10:04
  • Yes when you pay me, this is really simple and easy. Do some research. – rckoenes Dec 06 '11 at 10:19
0

Issue is just simple you are trying to save edited image into bundle.Actually application bundle is "Read-Only". For this kind of Read and Write access you can use either document directory or Library. You can get document directory path by following snippet of code.

NSString *path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
Iducool
  • 3,543
  • 2
  • 24
  • 45