12

I am showing very big size images in UITableView, because of this my app is getting crashed after some time. Now i want to resize the image and save it to disk. can some one help me resizing the image from NSData/UIImage and saving saving it to the disk.

I got the code to resize the image from UIImage, so as the result i have my resized in image in UIImage object, how to save it to iphone sandbox.

Thanks,

nbojja
  • 1,665
  • 7
  • 28
  • 38

3 Answers3

30

Here is code to save to the Documents directory on iOS that is from working code.

// Convert UIImage to JPEG
NSData *imgData = UIImageJPEGRepresentation(image, 1); // 1 is compression quality

// Identify the home directory and file name    
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"]; 

// Write the file.  Choose YES atomically to enforce an all or none write. Use the NO flag if partially written files are okay which can occur in cases of corruption
[imgData writeToFile:jpgPath atomically:YES]; 
zekel
  • 9,227
  • 10
  • 65
  • 96
Praxiteles
  • 5,802
  • 9
  • 47
  • 78
  • Awesome answer, it helped me. Is it simple (one line of code) to access that data again? Or should I ask another question on stack? – SirRupertIII May 21 '13 at 03:21
  • 1
    @KKendall you can just do `[NSData dataWithContentsOfFile:]` or `[UIImage imageWithContentsOfFile:]` – Liron Aug 07 '13 at 05:00
  • 1
    JPEG is a lossy format, so I recommend using UIImagePNGRepresentation instead. You will have quality issues if you write/read the image to/from disk repeatedly. – jeremywhuff Feb 21 '14 at 21:01
4

You're asking 'how do I write a file' right?

I guess there is a touch of complication in that you probably want to write into the Library/Caches directory, so when the phone gets backed up you don't save those files.

Get the root of the app folder w/ NSHomeDirectory() and then append Library/Caches.

You can write an NSData to the fs w/ a method on NSData. If you have a UIImage, you can do UIImageJPEGRepresentation() or UIImagePNGRepresentation to get data.

Andrew Pouliot
  • 5,423
  • 1
  • 30
  • 34
  • 6
    You probably want to use NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDirectory = [paths objectAtIndex:0]; Rather than appending 'Library/Caches' yourself. – Jesse Rusak Mar 31 '10 at 12:05
1

NSData *imgData = UIImageJPEGRepresentation(image, 1);

CGImageSourceRef source = CGImageSourceCreateWithData((CFMutableDataRef)imgData, NULL);
NSDictionary *metadata = [(NSDictionary *) CGImageSourceCopyPropertiesAtIndex(source,0,NULL)autorelease];
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
[imageMutableData writeToFile:jpgPath atomically:YES];

It will copy your image to documents folder in sandbox with name Test.jpg.

If you want to do it for PNG you can try UIImagePNGRepresentation(image);

kv0
  • 914
  • 10
  • 14
Rajat
  • 1,378
  • 4
  • 17
  • 33