【问题标题】:IOS - gif file was saved as jpegIOS - gif 文件被保存为 jpeg
【发布时间】:2014-12-15 17:45:08
【问题描述】:

我使用AFNetworking 2.0从网络下载了一个gif图像,然后使用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"];
    }
}];

然后我尝试使用UIImagePickerViewController从相机中检索此图像,但我检索到的图像不是GIF图像而是带有参考网址的jpeg图像:

UIImagePickerControllerReferenceURL = "assets-library://asset/asset.JPG?id=2E7C87E4-5853-4946-B86B-CC8AAF094307&ext=JPG";

不知道故障是ALAssetsLibrary还是UIImagePickerViewController,如何超越

【问题讨论】:

    标签: ios image gif


    【解决方案1】:

    照片库不支持 GIF。

    它支持PHAssetMediaTypeImage(JPG)、PHAssetMediaTypeVideo(MOV)或PHAssetMediaTypeAudio(可能是M4A,这里不确定)。

    https://developer.apple.com/library/ios/documentation/Photos/Reference/Photos_Constants/index.html#//apple_ref/c/tdef/PHAssetMediaSubtype

    【讨论】:

    • Photos 框架对格式保存的支持有些有限并不意味着 Library 有。但是,PHAssetMediaTypeImage 不限于 JPEG,这是保存的 PNG、TIFF 和,是的,GIF 可以显示在您的照片应用程序中的方式。实际上,您参考的文档仅说“资产是照片或其他静态图像”,根本没有提及 JPEG。
    【解决方案2】:

    writeImageToSavedPhotosAlbum: 方法仅将静止图像保存为 JPEG,新的 Photos 方法也是如此。但是,有种方法可以保存其他格式,包括(是的!)GIF。

    您无需处理 CGImageRefs — 只需获取 GIF 数据,然后使用 writeImageDataToSavedPhotosAlbum:metadata:completionBlock: 方法保存即可。像这样的:

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

    见this answer。

    如果您想生成一个 GIF,它会稍微复杂一些,但简单地保存一个就很简单了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-26
      • 2016-01-28
      • 1970-01-01
      • 2018-11-12
      • 2011-12-30
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多