一、有损
将其转换成UIImage对象,再通过UIImageJPEGRepresentation或UIImagePNGRepresentation方法转成NSData
这种方法不推荐,因为这个其实对图片进行了压缩转换,不再是图片的原始数据(像如果是gif动态图会变成静态图片),同时照片原来附带的拍摄信息什么的也会丢失。
let myAsset = ALAsset()
guard var representation = myAsset.defaultRepresentation() else {
return
}
var image = UIImage(cgImage:representation.fullResolutionImage().takeUnretainedValue())
var data = image.jpegData(compressionQuality: 1.0)
一、无损
直接复制图片的字节数据
let myAsset = ALAsset()
guard var representation = myAsset.defaultRepresentation() else {
return
}
var imageBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(representation.size()))
var bufferSize = representation.getBytes(imageBuffer, fromOffset: 0, length: Int(representation.size()), error: nil)
var data:NSData = NSData(bytesNoCopy:imageBuffer ,length:bufferSize, freeWhenDone:true)
网友评论