IPhone stores image from image to file

In my iPhone application, I collect an image from the iPhone image library as well as from the device’s camera, and I display this image in imageView.

I can save my image in the iPhone image library.

But now I want to save my image in some directory with a specific name, so I can use this image again in my application, and I also want to save it in the sqlite file.

+8
sqlite objective-c iphone uiimagepickercontroller uiimageview
source share
2 answers

It is briefly written 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 this 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 save it to the SQLite database, you must take the code that creates the NSData object (either UIImageJPEGRepresentation or UIImagePNGRepresentation ) and save them in the BLOB column.

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

Documentation: UIImageJPEGRepresentation

If you are working with PNG, there is another method called UIImagePNGRepresentation .

+3
source share

Source: https://habr.com/ru/post/650875/


All Articles