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.
Related
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.
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
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];
When I save a video in Photos Album by using UISaveVideoAtPathToSavedPhotosAlbum, how can I retrieve it's new asset URL in assets-library://asset/asset.mov.... format
//outputURL.path is : file:///private/var/mobile/Applications/4535724C-7ABD-4F00-A363-9A62022F8EB0/tmp/trim.E8CD7632-7C52-4EA4-A462-8C5131B214AA.MOV.exp.mov
UISaveVideoAtPathToSavedPhotosAlbum(outputURL.path, self, #selector(video:didFinishSavingWithError:contextInfo:), nil);
Instead of UISaveVideoAtPathToSavedPhotosAlbum, you can use the -[ALAssetsLibrary writeVideoAtPathToSavedPhotosAlbum:(NSURL *)videoPathURL completionBlock (ALAssetsLibraryWriteVideoCompletionBlock)completionBlock] method (Apple Documentation here)
For example:
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:videoPathURL
completionBlock:^(NSURL *assetURL, NSError *error)
{
/* process assetURL */
}];
Important Note: The important thing to remember when dealing with ALAssetsLibrary is that the assetURL is only valid for the lifetime of the ALAssetsLibrary instance. So ensure you hold a reference to library until after you have finished processing the assetURL and any associated ALAsset.
I am using the following code to save image into photo album,
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[my_Image CGImage] orientation:(ALAssetOrientation)[my_Image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
Failure
} else {
Success
}
}];
[library release];
The above code is working fine in all the iPad other than iPad mini.
Actually i dont have iPad mini device. But my client saying that issue. I unable to find the error what actually happened in iPad mini. So how to fix this issue. Thanks.
Add check for ALAuthorizationStatus.
If ALAuthorizationStatus is ALAuthorizationStatusRestricted or ALAuthorizationStatusDenied then your image will not get stored in Photo album.
To check ALAuthorizationStatus. Use following:
ALAuthorizationStatus authorize = [ALAssetsLibrary authorizationStatus];