Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 678
ios Avatar asked Jan 10 '11 14:01

ios


2 Answers

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.

like image 90
theChrisKent Avatar answered Sep 22 '22 19:09

theChrisKent


NSString  *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/ImageFileName.jpg"];
[UIImageJPEGRepresentation(yourUIImageView.image,1.0) writeToFile:path atomically:YES];

Documentation: UIImageJPEGRepresentation

If you're handling PNGs there's another method called UIImagePNGRepresentation.

like image 21
Björn Avatar answered Sep 23 '22 19:09

Björn