xcode 将生成的 GIF 保存到相机胶卷?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26301031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save generated GIF to camera roll?
提问by Rob Caraway
Thanks for reading. I've created a GIF using methods from this question:
谢谢阅读。我使用这个问题中的方法创建了一个 GIF:
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:
我试图使用看似能够给非JPG / PNG图像保存到相机胶卷,唯一的方法ALAssetLibrary
的writeImageDataToSavedPhotosAlbum:metadata:completionBlock:
I save the Gif to the temp Directory like this:
我将 Gif 保存到临时目录,如下所示:
NSString *exportPath = [NSTemporaryDirectory() stringByAppendingString:@"/animated.gif"];
NSURL *fileURL = [NSURL fileURLWithPath:exportPath isDirectory:NO];
Then access the NSData
like:
然后访问NSData
类似:
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).
因为我能够在显示它的GIF创建UIImageView
,但是当我试图挽救它,该方法返回一个成功(没有错误),但不实际保存(返回Nil
为NSURL * assetURL
并没有出现在摄像头卷)。
How can I get my GIFs to save successfully to the camera roll?
如何让我的 GIF 成功保存到相机胶卷?
采纳答案by Rob Caraway
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.
我发现问题是我无法从文件中实际获取 GIF。我从使用CGImageDestinationCreateWithURL
to切换到CGImageDestinationCreateWithData
使用 aCFMutableDataRef
来保存 Gif 数据。我不知道为什么,但这可以节省到相机胶卷中writeImageDataToSavedPhotosAlbum
。
回答by bllakjakk
**
**
Solution 01 : Only saving the existing GIF file to Camera Roll
解决方案 01:仅将现有的 GIF 文件保存到相机胶卷
**
**
As I understand your problem. You are able to generate a GIF file but cannot save and also view it to the Camera Roll.
据我了解你的问题。您可以生成 GIF 文件,但无法将其保存并查看到相机胶卷。
So I am attaching a sample test using existing GIF File.
所以我使用现有的 GIF 文件附加了一个示例测试。
Step 01.I copied a gif IMG_0009.GIF file in my Application Document directory.
步骤 01。我在我的应用程序文档目录中复制了一个 gif IMG_0009.GIF 文件。
Step 02Than I use the below code to load this files NSData:
步骤 02比我使用下面的代码加载这个文件 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:
步骤 03:现在我将文件保存在媒体目录中:
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.
资产 URL 是正确的。现在您可以检查您的媒体目录。您可以找到保存的 gif 图像。
Have Fun :)
玩得开心 :)
**
**
Solution 02: Demo of Creating and saving GIF to Camera roll
解决方案 02:创建和保存 GIF 到相机胶卷的演示
**
**
I cloned some solution to show creating and saving of GIF files to Camera Roll. You can download and check my fork at github:
我克隆了一些解决方案来展示创建 GIF 文件并将其保存到相机胶卷。你可以在github下载并查看我的fork:
The demo creates a GIF file by taking 2 or more images and save in the Camera Roll Directory
该演示通过拍摄 2 个或更多图像创建一个 GIF 文件并保存在相机胶卷目录中
https://github.com/bllakjakk/Giraffe
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
它使用我在http://jitsik.com/wordpress/?p=208之前的解决方案中提到的库
How to verify:
如何验证:
Step 01:Run the demo project.
步骤 01:运行演示项目。
Step 02:As directed by the application add 2 images and click Export.
步骤 02:按照应用程序的指示添加 2 个图像,然后单击导出。
Step 03:Now check the camera roll you will find the created gif.
步骤 03:现在检查相机胶卷,您将找到创建的 gif。
Previous:
以前的:
GIF is a proprietary format, so you would need a 3rd party lib to save it.
GIF 是一种专有格式,因此您需要一个 3rd 方库来保存它。
check following link: http://jitsik.com/wordpress/?p=208
检查以下链接:http: //jitsik.com/wordpress/?p=208
回答by Steve Sheets
Has this been updated to work with iOS 9, and the deprecation of ALAssets? I do not see similar calls in PHPhotoLibrary.
这是否已更新为适用于 iOS 9,以及 ALAssets 的弃用?我在 PHPhotoLibrary 中没有看到类似的调用。
回答by Garcia Hotspur
Here is an updated answer using PHPhotoLibrary, since ALAssetsLibrary is deprecated.
这是使用 PHPhotoLibrary 的更新答案,因为 ALAssetsLibrary 已被弃用。
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:
如果你需要从 URL 下载 GIF 数据,你可以使用这个:
NSData *gifData = [NSData dataWithContentsOfURL:theGIFsURL];