iOS compress jpg not in a right size - ios

I try to compress a jpg with 0.4 quality. I printed the data size using NSlog, it shows the NSData size is 337613, but after save to album, the file was increased to 677947.
What can I do with this?
PS: image object is from Camera.
NSData *newData =UIImageJPEGRepresentation(image,0.3f);
UIImage *compressdImg30 = [UIImage imageWithData:newData];
NSLog(#"[after 0]newData.length=%u",[newData length]); // print 337613 in debug
UIImageWriteToSavedPhotosAlbum(compressdImg30,nil,nil,nil);
then I plug iPhone and copy the file to my Mac Mini, and type "ls -ltr" to see the file size, then showing 677947.

You can use ALAssetsLibrary's
-writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
method to save image data to library.
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
[al writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Completion block/Do check the error if any");
}];
Try it and see

Related

How to save cv::mat in opencv c++ function to image file in IOS for debugging [duplicate]

I have used this code from the IOS examples. And tried to save the images from camera. The issue is that the image is getting saved but with a blue tint as below.
The below is the code I used to save the image.
- (void)processImagecv::Mat&image
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage * convertedImage = [ViewController imageWithCVMat:image];
[library writeImageToSavedPhotosAlbumconvertedImage CGImage] orientationALAssetOrientation)[convertedImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error)
{
// TODO: error handling
}
else
{
// TODO: success handling
NSLog(#"Success";
}
}];
TS(DetectAndAnimateFaces);
faceAnimator->detectAndAnimateFaces(image);
TE(DetectAndAnimateFaces);
}
In a given image order of channels is reversed, ie instead from opencv default representation BGR the image has the representation RGB, namely channel red and blue are swapped.

Creating an image from NSData and saving it using PHPhotoLibrary

I am tring to create an image using the NSData collected from Bluetooth. The data is saved in NSData variable recdata which is then converted to image using imageWithData. I am trying to save this image to the Photo library using the code below. But I don't see the image in the photo library. Am I missing something??
Also, as I am doing this project where I am receiving the data from bluetooth, I don't know how to check the error message on my iPhone screen. Currently, I am using the [information setText:[NSString stringWithFormat:#"%#", error.localizedFailureReason ]]; from the completionhandler to print the error message. But nothing gets printed and the app crashes and closes down.
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
//PHAssetResourceType assetType = PHAssetResourceTypePhoto;
UIImage *image = [UIImage imageWithData:recdata];
[PHAssetChangeRequest creationRequestForAssetFromImage: image];
[information setText:[NSString stringWithFormat:#"Krishna"]];
//PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAssetFromImage: image];
//PHAssetResourceCreationOptions *creationOptions = nil;
//creationOptions.originalFilename = command1; // Name of the file to be taken from command1
//[request addResourceWithType:assetType data:rxdata options:creationOptions]; //rxdata has all the data needed for the file to be transferred to the final image file.
} completionHandler:^(BOOL success, NSError *error) {
[information setText:[NSString stringWithFormat:#"%#", error.localizedFailureReason ]];
}];

IOS - gif file was saved as jpeg

I downloaded an gif image from the network using AFNetworking 2.0 then save it to camera roll using ALAssetsLibrary
[assetsLibrary writeImageToSavedPhotosAlbum:[responseObject CGImage] orientation:(ALAssetOrientation)[responseObject imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error)
{
if (error)
{
[App showAlertWithTitle:#"Error" message:#"Save message failed"];
}
else
{
[App showAlertWithTitle:#"Success" message:#"Saved success"];
}
}];
Then I tried to retrieve this image from camera using UIImagePickerViewController, but the image I retrieved was not a GIF image but a jpeg image with reference url:
UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=2E7C87E4-5853-4946-B86B-CC8AAF094307&ext=JPG";
I don't know whether the fault is ALAssetsLibrary or UIImagePickerViewController and how to surpass it
The photo library does not support GIFs.
It has support for PHAssetMediaTypeImage (a JPG), PHAssetMediaTypeVideo (a MOV), or PHAssetMediaTypeAudio (probably an M4A, not sure here).
https://developer.apple.com/library/ios/documentation/Photos/Reference/Photos_Constants/index.html#//apple_ref/c/tdef/PHAssetMediaSubtype
The writeImageToSavedPhotosAlbum: methods only save still images as JPEGs, as do the new Photos methods. However, there are ways of saving other formats, including (yes!) GIF.
You don't need to mess about with CGImageRefs—just grab the GIF data and then save it, using the writeImageDataToSavedPhotosAlbum:metadata:completionBlock: method. Something like this:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSData *data = [NSData dataWithContentsOfURL:
[NSURL URLWithString:#"http://somewhere/something.gif"]]];
[library writeImageDataToSavedPhotosAlbum:data
metadata:nil
completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
[App showAlertWithTitle:#"Error" message:#"Save message failed"];
} else {
[App showAlertWithTitle:#"Success" message:#"Saved success"];
}
}];
See this answer.
If you want to generate a GIF, it's somewhat more complex, but simply saving one is straightforward.

Save generated GIF to camera roll?

Thanks for reading. I've created a GIF using methods from this question:
Create and and export an animated gif via iOS?
I'm trying to use the only method that appears to be able to save non JPG/PNG images to the camera roll, ALAssetLibrary's writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
I save the Gif to the temp Directory like this:
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:#"/animated.gif"];
NSURL *fileURL = [NSURL fileURLWithPath:exportPath isDirectory:NO];
Then access the NSData like:
NSData * gifData = [NSData dataWithContentsOfFile:fileURL.absoluteString];
The GIF is created as I'm able to display it in a UIImageView, but when I try to save it, the method returns as a success (no error) but doesn't actually save (returns Nil for the NSURL * assetURL and does not appear in the camera roll).
How can I get my GIFs to save successfully to the camera roll?
**
Solution 01 : Only saving the existing GIF file to Camera Roll
**
As I understand your problem. You are able to generate a GIF file but cannot save and also view it to the Camera Roll.
So I am attaching a sample test using existing GIF File.
Step 01. I copied a gif IMG_0009.GIF file in my Application Document directory.
Step 02 Than I use the below code to load this files NSData:
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:#"IMG_0009.gif"];
NSData *gifData = [NSData dataWithContentsOfFile:[fileURL path]];
Step 03: Now I save the file in the Media Directory:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageDataToSavedPhotosAlbum:gifData metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Success at %#", [assetURL path] );
}];
The Asset URL is proper. Now you can check you media directory. you can locate the saved gif image.
Have Fun :)
**
Solution 02: Demo of Creating and saving GIF to Camera roll
**
I cloned some solution to show creating and saving of GIF files to Camera Roll.
You can download and check my fork at github:
The demo creates a GIF file by taking 2 or more images and save in the Camera Roll Directory
https://github.com/bllakjakk/Giraffe
The main Code to focus is like below:
[export encodeToFile:tempFile callback:^(NSString * aFile) {
NSLog(#"Path: %#", aFile);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSData *data = [NSData dataWithContentsOfURL:[[NSURL alloc]initFileURLWithPath:aFile]];
[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"Success at %#", [assetURL path] );
}];
}];
It uses the library as I mentioned in my solution before http://jitsik.com/wordpress/?p=208
How to verify:
Step 01: Run the demo project.
Step 02: As directed by the application add 2 images and click Export.
Step 03: Now check the camera roll you will find the created gif.
Previous:
GIF is a proprietary format, so you would need a 3rd party lib to save it.
check following link: http://jitsik.com/wordpress/?p=208
I found the issue was that I was unable to actually grab the GIF from the file. I switched from using CGImageDestinationCreateWithURL to CGImageDestinationCreateWithData and used a CFMutableDataRef to hold the Gif data. I don't know why, but that made saving to camera roll with writeImageDataToSavedPhotosAlbum work.
Has this been updated to work with iOS 9, and the deprecation of ALAssets? I do not see similar calls in PHPhotoLibrary.
Here is an updated answer using PHPhotoLibrary, since ALAssetsLibrary is deprecated.
I used this answer from another user - 陈星旺
PHPhotoLibrary save a gif data
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:#"/animated.gif"];
NSURL *fileURL = [NSURL fileURLWithPath:exportPath isDirectory:NO];
NSData * gifData = [NSData dataWithContentsOfFile:fileURL.absoluteString];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
[[PHAssetCreationRequest creationRequestForAsset]
addResourceWithType:PHAssetResourceTypePhoto
data:gifData
options:options];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
if (success) {
NSLog(#"image saved!");
} else {
NSLog(#"error saving image - %#", error ? error.localizedDescription : #"");
}
}];
If you needed to download the GIF data from a URL, you could use this:
NSData *gifData = [NSData dataWithContentsOfURL:theGIFsURL];

iOS - Saving GIF from URL to Saved Photos album

I am receiving from a webservice an animated gif url.
how can i save that gif image to photo album ?
What i did is downloading the data and converting it to UIImage using category helper
UIImage* gifImage = [UIImage animatedImageWithAnimatedGIFURL:[NSURL
URLWithString:self.resultImageUrl]];
and after that saving it useing
writeImageToSavedPhotosAlbum
but the image is saved as first frame.
so i thought to try and save directly the NSData using
writeImageDataToSavedPhotosAlbum
But i can't find any documentation about what to put in the metadata of the image so the album will know it's gif file.
Bottom line, I want the gif file will be visible in the user photo album and when he will send it using email client it will send it as gif animation and not just first frame
Please advise,
Thanks
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSData *data = [NSData dataWithContentsOfURL:[self getCurrentGIFURL]];
[library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {}
See this answer

Resources