Saving images to the Photo Library?

Discussion in 'iOS Programming' started by printf, Aug 2, 2009.

  1. printf macrumors regular

    Aug 27, 2008
    my googling has turned up nothing (that works).

    i want to save the UIImage using UIImageJPEGRepresentation, but i'm unable to get it to work in in the iPhone simulator. it doesn't crash, but it doesn't write the image either...

    NSData * imageData = UIImageJPEGRepresentation(image.image, 1.0);
    NSString *path = [[NSString alloc] initWithString:@"~/Library/Application Support/iPhone Simulator/User/Media/DCIM/100APPLE/SAVE_IMAGE_TEST1.JPG"];
    [imageData writeToFile:path atomically:YES];	
    first off, is it even possible to do this in simulator?? if so, can i please get an example? ideally it would save to the Photo Library that can be accessed in the simulator as i want this to be as close to the real deal as possible, but anywhere on my hard disk will work if this isn't possible.

    second, i'm using the imagePickerController to grab an image and then edit it, but when i save i want to save to the current directory where the image came from, but use a different file name of course. how to i get the current directory?

  2. kainjow Moderator emeritus


    Jun 15, 2000
    I think you might want the UIImageWriteToSavedPhotosAlbum() function..

    However, the reason your data isn't getting written to (at minimum) is because you're using a relative path via ~ and you need to use absolute paths. For testing just user the full path, e.g. /Users/you/Library/... etc.
  3. printf thread starter macrumors regular

    Aug 27, 2008
    hi kainjow,

    the problem i'm having with UIImageWriteToSavedPhotosAlbum, is that first, i don't know how to specify a name for the saved image, but more importantly, it saves the image as a highly compressed JPEG even when i call use UIIMagePNGRepresentatin to create the image data. here's my code:

    //will result in highly compressed (very lossy image)
    NSData * imageData = UIImagePNGRepresentation(image.image);
    UIImage *theImage = [UIImage imageWithData:imageData];
    UIImageWriteToSavedPhotosAlbum(theImage, self, nil, nil);
  4. kainjow Moderator emeritus


    Jun 15, 2000
    Old thread, I know, but I just saw on the Cocoa-dev mailing list that UIImageWriteToSavedPhotosAlbum() saves as a JPEG, so transparency will be lost. Looks like there's no workaround.

Share This Page