• 保存网络图片、视频到手机相册


    1.保存网络图片到手机相册
     
    - (void)toSaveImage:(Nsstring *)urlString {
    
        NSURL *url = [NSURL URLWithString: urlString];
        SDWebImageManager *manager = [SDWebImageManager sharedManager];
        UIImage *img;
        if ([manager diskImageExistsForURL:url])
        {
            img =  [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
        }
        else
        {
            //从网络下载图片
            NSData *data = [NSData dataWithContentsOfURL:url];
            img = [UIImage imageWithData:data];
        }
        // 保存图片到相册中
        UIImageWriteToSavedPhotosAlbum(img,self, @selector(image:didFinishSavingWithError:contextInfo:),nil);
        
    }
    //保存图片完成之后的回调
    - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
      contextInfo:(void *)contextInfo
    {
        // Was there an error?
        if (error != NULL)
        {
            // Show error message…
            [self showHintMiddle:@"图片保存失败"];
        }
        else  // No errors
        {
            // Show message image successfully saved
            [self showHintMiddle:@"图片保存成功"];
        }
    }
    

    2.保存网络视频到手机相册

    //-----下载视频--
    - (void)playerDownload:(NSString *)url{
        
        [self showWaitHint:@""];
        
        DDLog(@"urlurlurlurl===%@",url);
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString  *fullPath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"jaibaili.mp4"];
        NSURL *urlNew = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:urlNew];
        NSURLSessionDownloadTask *task =
        [manager downloadTaskWithRequest:request
                                progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
                                    return [NSURL fileURLWithPath:fullPath];
                                }
                       completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
                           DDLog(@"%@",response);
                           [self saveVideo:fullPath];
                       }];
        [task resume];
        
    }
    
    //videoPath为视频下载到本地之后的本地路径
    - (void)saveVideo:(NSString *)videoPath{
        
        if (videoPath) {
            NSURL *url = [NSURL URLWithString:videoPath];
            BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([url path]);
            if (compatible)
            {
                //保存相册核心代码
                UISaveVideoAtPathToSavedPhotosAlbum([url path], self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
            }
        }
    }
    
    
    //保存视频完成之后的回调
    - (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError: (NSError *)error contextInfo: (void *)contextInfo {
        if (error) {
            NSLog(@"保存视频失败%@", error.localizedDescription);
            [self hideHUD];
            [self showHintMiddle:@"视频保存失败"];
        }
        else {
            NSLog(@"保存视频成功");
            [self hideHUD];
            [self showHintMiddle:@"视频保存成功"];
        }
    }
    

    注意:此处设计到app权限,需在info.plistw文件里面增加  Privacy - Photo Library Additions Usage Description   ,不然会崩溃。

  • 相关阅读:
    《Effective Java》 读书笔记(三) 使用私有构造方法或枚举实现单例类
    《Effective Java》 读书笔记(二) 在构造参数过多的时候优先考虑使用构造器
    读书笔记-《Maven实战》-2018/5/3
    读书笔记-《Maven实战》-关于Maven依赖传递的思考 2018/4/26
    MySQL基础篇(07):用户和权限管理,日志体系简介
    SpringCloud微服务:Sentinel哨兵组件,管理服务限流和降级
    MySQL基础篇(06):事务管理,锁机制案例详解
    Java并发编程(02):线程核心机制,基础概念扩展
    SpringBoot2 整合ElasticJob框架,定制化管理流程
    Java基础篇(02):特殊的String类,和相关扩展API
  • 原文地址:https://www.cnblogs.com/OIMM/p/13601879.html
Copyright © 2020-2023  润新知