How To Save An Image

Discussion in 'iOS Development' started by Steaps, Mar 12, 2009.

  1. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I'm looking for a way to save a UIImage so i can retrieve it next time the application opens. I believe you need to use the applications Documents directory, or if jailbroken create a folder somewhere on the device. Or should i do this with NSUserDefaults somehow?

    Thanks, Stephen.4.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You can save the data in NSUserDefaults, but you will need to load that image every time the NSUserDefaults are referenced. That is bad.

    [EDIT: Although, there are times when saving small images to NSUserDefaults isn't all that bad. Download my Pocket Elf form the AppStore, adn use it for a bit. Then. go into its Library directory to see sample images saved to a defaults file.]

    You can save images wherever you want, you are not limited to the Documents directory. Put it into Library, tmp, or the application bundle itself, if you want. I use Documents, personally, beasue it helps keep things organised, and separates app created images form app images. If it's not something that you want to hang onto, use tmp.

    Saving an image is super easy:
    Code:
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(yourUIImage)];
    [imageData writeToFile:pathToImageFile atomically:YES];
    Alternatively, use the UIImageJPGRepresentation(args)
    Code:
    CFFloat compressionQuality = 1.0;  //0.0 being the maximum compression, 1.0 being the best quality
    NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(yourUIImage, compressionQuality)];
    [imageData writeToFile:pathToImageFile atomically:YES];
  3. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Thank you!
    pathToImageFile would/could be an NSString, correct?
    Code:
    NSString *pathToImageFile = @"/var/mobile/Library/Folder";
    (I had this code yesterday when looking for this, just looking to find it now).
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Except, you should never hardcode a path.
    Code:
    NSString *unexpandedPath = [[NSString alloc] initWithString:@"~/Library/YoutFolder/YourImage.png"];
    NSString *imagePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];

    EDIT: Although, I would only have the path to ~/Library/YourFolder and have the NSString pathWithComponts: methos add the image name.
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Wow first time, I've seen Skylar with spelling mistakes, is there something wrong?
  6. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I see, thank you. Didn't know i shouldn't hardcode it.
    How would i retrieve that Data, and recreate as an image? GetBytes?
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    What? It's just an image.


    You're creating an image. Just [UImage imageNamed:whatever], or [UIImage imageWithContentsOfFile:whatever] if the image isn't withing the app bundle.


    If you really want to, you can use NSData to get the data of the png or jpg that you created and use [UIImage imageWithData:data];
  8. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Sorry. I mis-understood NSData.
    Thanks again.

    EDIT:

    This is causing my application to crash.
    Code:
    	unexpandedPath = [[NSString alloc] initWithString:@"/var/mobile/Library/CrackedScreen/crackedImage.png"];
    	imagePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];
    	NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(crackedImage)];
    	[imageData writeToFile:imagePath atomically:YES];
    
    Yes, the directory /CrackedScreen/ does exist. I create it with:
    Code:
    system("mkdir /var/mobile/Library/CrackedScreen");
    
    In applicationDidFinishLaunching.
    And i have checked, it does create the folder.
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    From irc:




    Now, what I would do is create an NSString variable that has the path of ~/Library/CrackedScreen as an ivar. That way
    you can refer to that directory whenever you need to. And build the path, using the method I showed you a couple posts
    up with the image name. That way you can refer to any image, or even have non statically named images.
  10. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I replaced
    @"/var/mobile/Library/CrackedScreen/crackedImage.png" with
    @"~/Library/CrackedScreen/crackedImage.png"
    The application still crashes.

Share This Page