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]
Related
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
I need a method to convert a UIImage in a NSString and then convert the NSString back to a UIImage.
Thanks.
for >= IOS 7
- (NSString *)imageToNSString:(UIImage *)image
{
NSData *imageData = UIImagePNGRepresentation(image);
return [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
}
- (UIImage *)stringToUIImage:(NSString *)string
{
NSData *data = [[NSData alloc]initWithBase64EncodedString:string
options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}
Convert it to a binary stream instead (NSData). This will depend on the format of your UIImage. If it's a JPEG/PNG for instance, you do:
NSData *data1 = UIImageJPEGRepresentation(image, 1.0);
NSData *data2 = UIImagePNGRepresentation(image);
UPDATE: Converting the binary data to NSString is a bad idea, that is why we have the class NSData. The OP wants to be able to send it as a data stream and then reconstruct it again; NSString will not be needed for this.
Convert to PNG or JPEG using UIImagePNGRepresentation or UIImageJPEGRepresentation, which will return an NSData, and then convert the NSData to a string (not sure how you want to do that mapping). How about just dealing with the NSData? You can read/write that to a file.
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);
I'm creating some temporary files in the iPad simulator. To test my file creation, I create the file and then read it back. Here's some code to show this:
-(NSString *) writeToTempFile:(UIImage*) image{
NSString *path = [self createTemporaryFile];
NSLog(#"path: %#", path);
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES];
free(data);
return path;
}
-(UIImage *) readTempFile:(NSString *) path{
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
UIImage *image = [[UIImage alloc] initWithData:data];
return image;
}
I call these methods one after another, before a final function writes out the UIImage to the photo album.
UIImageWriteToSavedPhotosAlbum(image2, self, nil, nil);
The problem is, this always crashes my app on the third time it is executed. First and second time it successfully does all of this and stores to the album. Third time it crashes to Home. Any ideas?
NSData *data = UIImageJPEGRepresentation(image, 1);
[data writeToFile:path atomically:YES];
free(data);
The NSData returned from UIImageJPEGRepresentation is -autoreleased. There is no need to free() it. And it is wrong to free() any Objective-C objects — send a -release message instead.
Please read through the Memory Management Programming Guide.
I want to add my UIImage directly into the file instead of converting into UIImagePNGRepresentation or UIImageJPGRepresentation(as it takes time) like:-
UIImage *im = [UIImage imageWithCGImage:ref];
[array addObject:im];
NSData *data = [array objectAtIndex:i];
[data writeToFile:path atomically:YES];
But it is showing error.
So there is any way that i can do it.
Thanks in Advance.
your use of the array only obfuscates that you are basically doing:
NSData *data = im;
Which cannot possibly work because im is a UIImage, and not an NSData nor a subclass.
What you want to do is to create a new NSData and initialize it with the content of the image. Since you got a CGImageRef, I suggest using it directly, without using a UIImage in between.
CGDataProviderRef imageDataProvider = CGImageGetDataProvider(ref);
CFDataRef imageData = CGDataProviderCopyData(imageDataProvider);
NSData *data = (NSData*) imageData;
Note that it is OK to cast the CFDataRef to NSData* because CFData is “toll-free bridged” with its Cocoa Foundation counterpart, NSData.
I hope that helps.
(don't forget to release data when done)