Search code examples
iphoneobjective-csqliteuiimageviewuiimagepickercontroller

iPhone store image from imageview to file


In my iPhone App I am picking image from iPhone Image Library as well from device camera, and I am displaying that image in imageView.

I am able to store my image into iPhone image Library.

But now I want to store my image in some Directory with specific name, so I can use that image again in my application and also I want to store it in sqlite file.


Solution

  • There is a short writeup of this here: http://iosdevelopertips.com/data-file-management/save-uiimage-object-as-a-png-or-jpeg-file.html

    Here is the relevant code stolen directly from that site:

    // Create paths to output images
    NSString  *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.png"];
    NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
    
    // Write a UIImage to JPEG with minimum compression (best quality)
    // The value 'image' must be a UIImage object
    // The value '1.0' represents image compression quality as value from 0.0 to 1.0
    [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
    
    // Write image to PNG
    [UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
    

    To store it in a SQLite Database you would take the code that makes an NSData object (either UIImageJPEGRepresentation or UIImagePNGRepresentation) and save them to a BLOB column.