I need to resize an UIImage and save it on my app file system,
I can resize the image
or just show it with a smaller cgrect for the frame,
but how can i save this newly resized image on my app file sys?
thanks!
I need to resize an UIImage and save it on my app file system,
I can resize the image
or just show it with a smaller cgrect for the frame,
but how can i save this newly resized image on my app file sys?
thanks!
Assuming your image is called image
//Where the 0 denotes the compression (0 to 1).
NSData *imageData = UIImageJPEGRepresentation(image, 0);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"MyImageFolder"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
NSString *fullPath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", imageName]]; //add our image to the path
bool successI = [imageData writeToFile:fullPath atomically:YES];
if (successI) {
// NSLog(@"Image saved correctly");
} else
NSLog(@"Error while saving Image");
Forgot to mention, you can change the
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
to save it somewhere else.
You can first convert the UIImage to NSData by
UIImageJPEGRepresentation
or
UIImagePNGRepresentation
Then write the data to Document or Library folder in your app.