iOS - Saving GIF from URL to Saved Photos album - ios

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

Related

iOS compress jpg not in a right size

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

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];

save image to specific album in photolibrary ios

I am creating app to download images from net and save it to Custom Album in PhotoLibrary.
i have successfully save images to Custom Album. But, The Problem is That images saved in Custom Album are also stored in SavedPhotos Folder!
i just want to store that image in my custom album!
my code is
ALAssetsLibrary* libraryFolder = [[ALAssetsLibrary alloc] init];
[libraryFolder saveImage:img toAlbum:#"libraryFolder" withCompletionBlock:^(NSError *error)
{
NSLog(#"%#",error.description);
}
];

Get UIImage from URL like assets-library:/

I'm developing and iOS app for iPad and I'm using a Repository called Grabkit in order to get images from different services like Instagram and Flicker in addition to images from the Camera Roll. The problem is that when the user selects a picture from the roll I get and URL such this: assets-library://asset/asset.JPG?id=DCFB9E49-93AA-49E3-89C8-2EE64AE2C4C6&ext=JPG
I've tried some codes to get the image from this kind of paths but no one has worked, such as the following:
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
// Ask for the "Asset" for the URL. An asset is a representation of an image in the Photo application.
[library assetForURL:originalImage.URL
resultBlock:^(ALAsset *asset) {
// Here, we have the asset, let's retrieve the image from it
CGImageRef imgRef = [[asset defaultRepresentation] fullResolutionImage];
/* Instead of the full res image, you can ask for an image that fits the screen
CGImageRef imgRef = [[asset defaultRepresentation] fullScreenImage];
*/
// From the CGImage, let's build an UIImage
imatgetemporal = [UIImage imageWithCGImage:imgRef];
} failureBlock:^(NSError *error) {
// Something wrong happened.
}];
Is something in my code wrong? Must I try another code?

Resources