saving UIImage to file cost a lot of time - iphone

I'm trying to load pictures in iPhone Photos app, then save the selected pictures in to my app's folder, using ALAssetsLibrary, and have two issues:
1. the image file saved to disk is much bigger then original files in Photos app, for example, a picture is 2.8MB, but after saved to my app's folder, it's 6.4MB, following is the code:
CGImageRef cgImage = [localPhoto thumbnail];
NSString *path = #"/documents/test/1.jpeg";//the path is just an example
BOOL succ;
UIImage *image1 = [UIImage imageWithCGImage:cgImage];
NSData *data1 = UIImageJPEGRepresentation(image1, 1.0);
succ = [data1 writeToFile:path atomically:YES];
the above code(saving 6.4MB image to file) costs about 1.6seconds, is it normal? is there anyway to make it faster?

Try with PNG representation of the image.
NSData *data1 = UIImagePNGRepresentation(image1);
or else reduce the image quality or JPG.
NSData *data1 = UIImageJPEGRepresentation(image1, 0.5);

Related

IOS - Saved image not showing up on Photos app

I am making an iOS app that saves an image of given URL.
I used the following code.
- (IBAction)save:(id)sender {
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:photoUrl]]];
NSLog(#"%f,%f",image.size.width,image.size.height);
// Let's save the file into Document folder.
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(#"saving jpg");
NSString *jpegFilePath = [NSString stringWithFormat:#"%#/test.jpg",docDir];
NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality
[data2 writeToFile:jpegFilePath atomically:YES];
NSLog(#"saving image done");
}
The image is saved successfully after calling this method.
Since I use iPhone simulator, I can see the image is saved my local Library folder.
However, when I open Photos (iPhone navie app) from the simulator, it doesn't detect the image I just saved.
When I save an image from Safari and open Photos app, it detects the image correctly.
But why can't it detect the image I saved thru my ios app? Any solutions?
How come/why do you think or assume any image written to a random location should be known about by the Photos app? That's just nonsense. Use the UIImageWriteToSavedPhotosAlbum() function instead.

Inaccurate NSData size given in bytes

I took a 13.3MB image and added it to XCode. I'm aware that when compiling, XCode performs some tricks to bring down the file size. I did this, to test how big the image now was, after being converted into data:
UIImage *image = [UIImage imageNamed:#"image.jpg"];
NSData *data = UIImageJPEGRepresentation(image, 1.0);
NSLog(#"length: %i", data.length);
The length I got back was 26758066. If that's in bytes, then it reads to me as 26.7MB. How is the image so big suddenly? Is there another way for me to get the image in data form without going through UIImage first?
EDIT: Further testing reveals that this works, and brings out a data length of ~13.3MB - the expected amount:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"jpg"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSLog(#"length: %i", data.length);
What your code is doing is decompressing the image into memory and then recompressing as JPEG, with the highest quality ratio (q=1.0). That’s why the image is suddenly so big.
If you want to check up on the file as stored in the resource bundle, ask NSBundle for the full file path and use NSFileManager to read the file size. You can do the same thing by hand on your Mac, just take a look into the BUILD_PRODUCTS_DIR for your project.

How to reduce the image resolution and memory size when saving or while capturing an image in iphone

In my contacts application i used an image view to display the contact persons image,
in this process while saving the data user can save the contact person image as well (in the form of string, image file name).
I copy the images in sand box (Documents directory) and save the file names of images
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSString *DirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[[NSUserDefaults standardUserDefaults] setValue:storedPicsDict.contactImage forKey:#"oldContactPic"];
//To SET the NEw IMAGE images from directory path
CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef generatedUUIDString = CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
NSString* hashKey = [(NSString*)generatedUUIDString autorelease];
self.ContactImageFilePath = [NSString stringWithFormat:#"%#/%#.png",DirectoryPath,hashKey];
storedPicsDict.contactImage = self.ContactImageFilePath;
[contactPicture setImage:image forState:UIControlStateNormal];
isNewContactImage = true;
}
[picker dismissModalViewControllerAnimated:YES];
}
the respective save image will displayed in contact's information.
But w*hen i saved few more images more than 6/7 it causes memory warnings and the and app gets crashed/slow down.*
So i need to save images with LOW RESOUTION and LOW MEMORY SIZE,
How is it possilbe, thanks
1>Save the image in jpeg format so that image size gets reduced
NSData *imgData = UIImageJPEGRepresentation(your_UIImage_to_save, 0.5);
[imgData writeToFile:path_of_doc_dir_with_image_name atomically:YES];
2>
If you want to reduce the quality and size of the image you can use this code
CGSize newSize=CGSizeMake(50,50); // I am giving resolution 50*50 , you can change your need
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Hope this will help you..
how about using this
UIImage *small = [UIImage imageWithCGImage:original.CGImage scale:0.25 orientation:original.imageOrientation];
and save the smaller image to a tmp file path for uploading. Do the images need to be .png?
Otherwise, you may also try UIImageJPEGRepresentation to lower the image's quality.

Save images in a n ios app

I am creating a greeting card app.I have some existing templates and i also can create new greeting cards by adding cliparts ,textstyles etc.I have saved the image as a uiimage object.I am stuck at the point to save these images for future use.Please help.
To save a UIImage to file do the following
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/img.jpg"];
//Save
NSData* imageData = UIImageJPEGRepresentation(img, 1.0);
[imageData writeToFile:path atomically:YES];
//Load
NSData* imageData = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage imageWithData:imageData];
Check out this link.
How to save picture to iPhone photo library?
You can also save the image in iPhone file system by converting image to data and save data to file.
NSData *data = UIImagePNGRepresentation(yourImage);
[data writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile]

Save NSMutableData representing an image to the iPhone photo library

I have a NSMutableData containing a JPEG that I want to save in the device library.
I can do it converting it to an UIImage, but then it loses the EXIF data, so UIImaes should be avoided.
EDIT To reflect what I am trying to accomplish
I am using the iphone-exif library to edit the metadata of a picture in my main bundle. I want to save the resulting image in the photo library
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"P1080330" ofType:#"JPG"];
NSData *uiJpeg = [NSData dataWithContentsOfFile:filePath];
EXFJpeg* jpegScanner = [[EXFJpeg alloc] init];
[jpegScanner scanImageData: uiJpeg];
NSLog(#" EXIF_Make %# ", [ jpegScanner.exifMetaData tagValue: [NSNumber numberWithInt:EXIF_Make]]); // OUTPUTS PANASONIC
// Change camera maker to FairyGodmother
[jpegScanner.exifMetaData addTagValue: #"FairyGodmother" forKey:[NSNumber numberWithInt:EXIF_Make]];
NSMutableData *newImageData = [[NSMutableData alloc] init];
[jpegScanner populateImageData:newImageData];
// NOW I HAVE THE NSMUTABLEDATA CONTAINGING MY IMAGE WITH ALL MY EXIF DATA. HOW DO I PUT IT TO THE PHOTO LIBRARY?
UIImageWriteToSavedPhotosAlbum (which takes a UIImage) is the only documented path to getting an image in the device's photo library, as far as I know.
What is your concern with "losing EXIF data"? Are you talking about orientation? (UIImage should take care of that for you.) Or other EXIF data?
The only way you can get an image out of the photo library is as a UIImage also, so I'm not sure what you stand to gain by shoving a raw JPEG stream into the library.
To Write a File use:
[yourImage writeToFile:filePath atomically:YES];
[]'s