What is the best way to save CGImageRef to a png or jpeg file in iOS?
For a general cocoa application (not using UIkit), see: Saving CGImageRef to a png file? but this seems too long and clunky.
Using UIKit, the code becomes a simple 3-liner:
UIImage *uiImage = [UIImage imageWithCGImage:cgImage];
NSData *jpgData = UIImageJPEGRepresentation(uiImage, 0.9f);
[jpgData writeToFile:path atomically:NO];
Related
I want to take backup of images on cloud so I calculate MD5 of an image in iOS.The issue is Md5 differs when it is calculated in foreground and background of the app. This issue come only in iOS 9.1. I use asset library to fetch the images. Below function is used to get the data (Both data are different from each when the application is in foreground and in background)
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CGImageRef imgRef = [assetRep fullScreenImage];
UIImage *img = [UIImage imageWithCGImage:imgRef
scale:1.0f
orientation:(UIImageOrientation)assetRep.orientation];
NSData *data = UIImageJPEGRepresentation(img, 90);
Thanks in advance....
Have a look at ALAssetRepresentation-MD5 which calculates the md5 hash from an ALAssetRepresentation without creating a UIImage or using UIImageJPEGRepresentation. I assume that one of these UIKit-related steps is responsible for your issue, event though they should be thread-safe.
I am writing NSData to a file and saving it in the device's app documents folder. For that, is it possible to get thumbnail from ALAssetsRepresentation object in NSData format. If so, any helpful links to that?
I couldn't find anything similar, other than getting CGImageRef from ALAssetsRepresentation. I don't want CGImageRef format as I have to use UIImageJPEGRepresentation or UIImagePNGRepresentation to convert it to NSData.
Try this one
GImageRef iref = [myasset thumbnail];
if (iref)
{
UIImage *theThumbnail = [UIImage imageWithCGImage:iref];
NSData *thumnailData = UIImagePNGRepresentation(theThumbnail);
}
This question already has answers here:
How to convert all image format into .png format?
(2 answers)
Closed 8 years ago.
Hi can anyone help in converting jpg images fetching from server into png images in objective-c.
Actually the problem is through the app,we are sharing images on facebook,but when jpg image is uploading in server,the image is not shared in facebook.
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageurl]]];
I have to convert the imageurl to png format.
Please try below code:
UIImage *img; // Your image coming from server
NSData *pngData = UIImagePNGRepresentation(img); // Convert it in to PNG data
UIImage *pngImage = [UIImage imageWithData:pngData]; // Result image
after you retrieve your images/url save them this way:
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
Answer taken from here
I have an object ALAsset retrieved from ALAssetLibrary I want to extrapolate a compress JPEG in order to send it to a web services.
any suggestion where to start?
Edit:
I've found a way to get NSData out of the ALAsset
ALAssetRepresentation *rappresentation = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rappresentation.size);
NSUInteger buffered = [rappresentation getBytes:buffer fromOffset:0.0 length:rappresentation.size error:&err];
but I can't find a way to reduce the size of the image by resizing and compressing it.
My idea was to have something like:
UIImage *myImage = [UIImage imageWithData:data];
//resize image
NSData *compressedData = UIImageJPEGRepresentation(myImage, 0.5);
but, first of all, even without resizing, just using this two lines of code compressedData is bigger than data.
and second I'm not sure about what's the best way to resize the UIImage
You can use the
[theAsset thumbnail]
Or;
Compressing might result bigger files after some point, you need to resize the image:
+ (UIImage *)imageWithCGImage:(CGImageRef)cgImage scale:(CGFloat)scale orientation:(UIImageOrientation)orientation
It's easy to get the CGImage from an ALAssetRepresentation:
ALAssetRepresentation repr = [asset defaultRepresentation];
// use the asset representation's orientation and scale in order to set the UIImage
// up correctly
UIImage *image = [UIImage imageWithCGImage:[repr fullResolutionImage] scale:[repr scale] orientation:[repr orientation]];
// then do whatever you want with the UIImage instance
I want to send UIImage from my application to the server. I use ASIHTTPRequest. I'll send NSData but how to convert from UIImage to NSData?
If you need PNG data in your NSData you can use:
NSData *data = UIImagePNGRepresentation(img);
Where img is your UIImage. There is a similar function for JPG.
The UIKit functions UIImageJPEGRepresentation() and UIImagePNGRepresentation() should do what you want.