UISaveVideoAtPathToSavedPhotosAlbum output - ios

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.

Related

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

Image is not saving to Photo album in iPad mini

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

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

Issue on save aif file format in ios

I tried to write the file format of "output.aif". I used the below code to write it to a path. The file got saved in two locations one at the path I specified and one more it got saved in the image gallery by default and the name of the file is of a random number. While exiting the application I use to delete the saved ".aif" files from the location I saved, But I couldnt delete the files that got saved in the gallery as I dont know the name of the files.
Is there any way to stop saving the "aif" files from gallery which is happening default.
Can any one suggest a different code to save the file in a location, which wont save the file in gallery?
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
}
else{
NSString *outputTp = [[[NSHomeDirectory() stringByAppendingString:#"/Documents/"] stringByAppendingString:#"output.aif"] retain];
inUrl = [url retain];
outUrl = [[NSURL fileURLWithPath:outputSound] retain];
reader = [[EAFRead alloc] init];
writer = [[EAFWrite alloc] init];
self.url = outputTp;
// this thread does the processing
[NSThread detachNewThreadSelector:#selector(processThread:) toTarget:self withObject:nil];
}
}];

Resources