0

I'm trying to save Images the user takes with the camera

1) If I use the UIImageWriteToSavedPhotosAlbum I can't seem to assign the fileName that I want. How, can you choose the file name?
The nice thing about using this option is
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
Then gives a thumbnail gallery of Photo Library directory.

2) which leads me to my next question Can
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
be used to get images from your own personal directory ?

3) Is there anyway to programmatically create sub folders within the Photo Library

4) Lastly,

NSArray  *paths    = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
if (![[NSFileManager defaultManager] fileExistsAtPath:[paths objectAtIndex:0] isDirectory:&isDir]) {
        NSError  *error;
        [[NSFileManager defaultManager] createDirectoryAtPath:[paths objectAtIndex:0 withIntermediateDirectories:YES attributes:nil error:&error];
}


Checking to see if the NSPicturesDirectory exists so I can write to it I keep getting a Cocoa error 513 not permitted

Thanks in advance

BarryF
  • 77
  • 1
  • 10
  • Not sure if this is what you mean by sub-folders, but if you mean photo albums with custom names, I used this category. http://www.touch-code-magazine.com/ios5-saving-photos-in-custom-photo-album-category-for-download/ – geraldWilliam Jun 18 '12 at 20:25
  • you need to save the images in photo library not in document directory, am i correct ? – Midhun MP Jun 18 '12 at 20:28
  • If it must be the photo library then my own album/sub directory. But then I need to create the album/sub directory from code. Same with Documents Dir – BarryF Jun 18 '12 at 21:29

2 Answers2

5

You can't assign a file name to photo library images. ios assign a file name called asset url to the images that are saved to the photo library, we can't change that.

If you need to save the images to photo library you can use ALAssetsLibrary

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
   [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)     [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
    if (error)
    {
     // Eror
    }
    else
    {
     // Success
    }
   }];
   [library release];

For more information check: How to save picture to iPhone photo library?

Save image in UIImageView to iPad Photos Library

Community
  • 1
  • 1
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
2

1.)

To save a filename of your choice, you need to save the image to your documents directory. This can easily be done like so:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 
{
    NSString *current = [NSString stringWithFormat:@"%@/%@",PHOTODATA_PATH,currentItem];

    UIGraphicsBeginImageContext(CGSizeMake(160,160));
    [image drawInRect:CGRectMake(0,0,160,160)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    NSData * thumbSize = UIImageJPEGRepresentation(scaledImage, 1.0);
    [thumbSize writeToFile:[NSString stringWithFormat:@"%@_thumb.jpg",current] atomically:YES];

    NSData * fullSize = UIImageJPEGRepresentation(image, 1.0);
    [fullSize writeToFile:[NSString stringWithFormat:@"%@.jpg",current] atomically:YES];

    [self dismissModalViewControllerAnimated:YES];
}

Of course this code may need to be edited to fit your exact situation but gives the basic understanding and way to save a photo to file using NSData.

2.)

Yes, UIImagePickerControllerSourceTypePhotoLibrary will access your Photo Library on your device. Any photos saved or taken from the camera roll will be accessible using this.

3.)

No, you cannot create subfolders in the Photo Library using your application. This can be done using iPhoto or iTunes or the like.

4.)

You only have access to the Documents and Library paths contained within your sandboxed environment. The only way you can save a photo to the Photo Library is by using the appropriate public methods. There is no need to check if the directory exists, the OS will take care of all of the "behind the scenes" tasks it needs to in order to manage the Photo Library.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186