保存GIF到系统相册

目前尝试的结果只有ALAssetsLibrary可以保存成功gif。保存成功后的GIF在系统相册为静态图,利用QQ或者微信即可看到动态效果。

OC:

-(void)saveGIFToPhone:(NSString*)uri{
// 请求网络图片并写入本地
  NSError *error;
  NSData *gifData;
  if([uri hasPrefix:@"http://"] || [uri hasPrefix:@"https://"]){
    gifData = [NSData dataWithContentsOfURL:[NSURL URLWithString:uri] options:NSDataReadingUncached error:&error];
  }else{
    gifData = [NSData dataWithContentsOfFile:uri];
  }
  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  NSDictionary *metadata = @{@"UTI":(__bridge NSString *)kUTTypeGIF};
  [library writeImageDataToSavedPhotosAlbum:gifData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
    if (error) {
      // "保存图片失败"
    }else{
      //保存图片成功"
    }
  }] ;
  }

RN:

RCT_REMAP_METHOD(saveGIFToPhone, uri:(NSString *)uri resolverBlock:(RCTPromiseResolveBlock)resolve rejecterBlock:(RCTPromiseRejectBlock)reject) {
  // 请求网络图片并写入本地
  NSError *error;
  NSData *gifData;
  if([uri hasPrefix:@"http://"] || [uri hasPrefix:@"https://"]){
    gifData = [NSData dataWithContentsOfURL:[NSURL URLWithString:uri] options:NSDataReadingUncached error:&error];
  }else{
    gifData = [NSData dataWithContentsOfFile:uri];
  }
  if (error) {
    reject(@"",@"",nil);
    return;
  }
  ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
  NSDictionary *metadata = @{@"UTI":(__bridge NSString *)kUTTypeGIF};
  [library writeImageDataToSavedPhotosAlbum:gifData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
    if (error) {
      // "保存图片失败"
      reject(@"",@"",nil);
    }else{
      //保存图片成功"
      resolve(nil);
    }
  }] ;
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页