• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

swift3.0保存图片到本地,申请权限

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.info中写上

<key>NSCameraUsageDescription</key>
<string>需要您的同意才能读取媒体资料库</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>需要您的同意才能读取媒体资料库</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>需要您的同意才能读取媒体资料库</string>
<key>Privacy - Photo Library Usage Description</key>
<string>需要您的同意才能读取媒体资料库</string>

 2.保存

import Photos

//操作结果枚举
enum PhotoAlbumUtilResult {
    case success, error, denied
}

//相册操作工具类
class PhotoAlbumUtil: NSObject {
    
     public typealias completion = ((_ result: PhotoAlbumUtilResult) -> ())
    //判断是否授权
    class func isAuthorized() -> Bool {
        return PHPhotoLibrary.authorizationStatus() == .authorized ||
            PHPhotoLibrary.authorizationStatus() == .notDetermined
        
    }
    
    //保存图片到相册
    class func saveImageInAlbum(image: UIImage, albumName: String,
                                completion: completion?) {
        //权限验证
        if !isAuthorized() {
            completion?(.denied)
            return
        }
        var assetAlbum: PHAssetCollection?
        
        //如果指定的相册名称为空,则保存到相机胶卷。(否则保存到指定相册)
        if albumName.isEmpty {
            let list = PHAssetCollection
                .fetchAssetCollections(with: .smartAlbum, subtype: .smartAlbumUserLibrary,
                                       options: nil)
            assetAlbum = list[0]
        } else {
            //看保存的指定相册是否存在
            let list = PHAssetCollection
                .fetchAssetCollections(with: .album, subtype: .any, options: nil)
            list.enumerateObjects({ (album, index, stop) in
                let assetCollection = album
                if albumName == assetCollection.localizedTitle {
                    assetAlbum = assetCollection
                    stop.initialize(to: true)
                }
            })
            //不存在的话则创建该相册
            if assetAlbum == nil {
                PHPhotoLibrary.shared().performChanges({
                    PHAssetCollectionChangeRequest
                        .creationRequestForAssetCollection(withTitle: albumName)
                }, completionHandler: { (isSuccess, error) in
                    self.saveImageInAlbum(image: image, albumName: albumName,
                                          completion: completion)
                })
                return
            }
        }
        
        //保存图片
        PHPhotoLibrary.shared().performChanges({
            //添加的相机胶卷
            let result = PHAssetChangeRequest.creationRequestForAsset(from: image)
            //是否要添加到相簿
            if !albumName.isEmpty {
                let assetPlaceholder = result.placeholderForCreatedAsset
                let albumChangeRequset = PHAssetCollectionChangeRequest(for:
                    assetAlbum!)
                albumChangeRequset!.addAssets([assetPlaceholder!]  as NSArray)
            }
        }) { (isSuccess: Bool, error: Error?) in
            if isSuccess {
                completion?(.success)
            } else{
                print(error!.localizedDescription)
                completion?(.error)
            }
        }
    }
}

 3.使用

PhotoAlbumUtil.saveImageInAlbum(image: image, albumName: "APP") { (result) in
    DispatchQueue.main.async {
        switch result{
        case .success:
            //"保存成功"
            break
        case .denied:
//没有权限,去设置里打开权限 let a = MyAlertController() a.addOKView("去设置"){ (a) in let url = URL(string: UIApplicationOpenSettingsURLString) if let url = url, UIApplication.shared.canOpenURL(url) { if #available(iOS 10, *) { UIApplication.shared.open(url, options: [:], completionHandler: { (success) in }) } else { UIApplication.shared.openURL(url) } } } a.addCancelView() a.show(self,title: "没有相册权限",message: "请您到 \"设置\" -> \"APP\" 开启相册权限", style: .alert ) break case .error: //"保存失败" } } }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
为什么说swift是面向协议编程--草稿发布时间:2022-07-18
下一篇:
Swift--方法(函数)发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap