 Swift从ALAsset中获取照片原图转NSData和相关用法

直接复制图片的字节数据(拿到原图数据)

let representation = resulttll?.defaultRepresentation() //resulttll为ALAsset属性
    
let imageBuffer = UnsafeMutablePointer.allocate(capacity: Int((representation?.size())!))
    
let bufferSize = representation?.getBytes(imageBuffer, fromOffset: Int64(0),
                                             length: Int((representation?.size())!), error: nil)

let datall:NSData =  NSData(bytesNoCopy:imageBuffer ,length:bufferSize!, freeWhenDone:true)

缩略图 (遍历相册,自定义相册时可以使用)
注意: thumbnail(小正方形的缩略图); aspectRatioThumbnail(按原始资源长宽比例的缩略图)

assetsLibrary.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: { (group, stop) in
        if group != nil {
            let model = AlbumModel()
            if group?.value(forProperty: ALAssetsGroupPropertyName) != nil {
                model.albumName = group?.value(forProperty: ALAssetsGroupPropertyName)as!String
            }else{
                model.albumName = ""
            }
            model.imageArray = NSMutableArray()
            //倒叙
            group?.enumerateAssets(options: NSEnumerationOptions.reverse, using: { (result, index, stop) in
                if result != nil {
                    let imageModel = ImageModel()
                    imageModel.isSelected = false
                    imageModel.image = UIImage(cgImage: (result?.thumbnail().takeUnretainedValue())!)
                    imageModel.pictureRest = result
                    model.imageArray.add(imageModel)
                    
                    self.currentImageArray = model.imageArray
                }
            })
            
            hud.hide(animated: true)
            self.dataArray.add(model)
        }else{
            for i in 0 ..< self.dataArray.count {
                let model = self.dataArray[i] as!AlbumModel
                self.titleArray.add(model.albumName)
                if model.albumName == "相机胶卷" {
                    self.selectedAlbumModel = self.dataArray[i] as! AlbumModel
                    self.currentIndex = i
                    self.currentImageArray = self.selectedAlbumModel.imageArray
                }
            }
            hud.hide(animated: true)
            self.albumTableView.dataArray = self.titleArray
            self.collectionView.reloadData()
        }
    }) { (error) in
        print("\(error)")
    }

你可能感兴趣的:( Swift从ALAsset中获取照片原图转NSData和相关用法)