I have tried to convert ALAsset to NSData in the following code:
let assetUrl = NSURL(string: self.filename!)
ALAssetsLibrary().assetForURL(assetUrl,
resultBlock: { asset in
let rep = asset.defaultRepresentation()
var buffer: UInt8 = UInt8()
var error: NSError?
let from: Int64 = 0
let length: Int = Int(rep.size())
var buffered = rep.getBytes(&buffer, fromOffset: from, length: length, error: &error)
let data = NSData(bytesNoCopy: &buffered, length: length, freeWhenDone: true)
self.video = data
}, // # 1
failureBlock: { error in
println("ReportPhoto::loadVideoFromAssetsLibrary() - Error while loading saved video from AssetsLibrary!")
}
)
But I am getting the following error message in the console (at # 1 in the code):
object 0x27d02ce8: pointer being freed was not allocated
I tracked the object mentioned in the error and found it, but I do not know what is the problem and how to solve it!
The object is a property inside the NSData object called _bytes
There are two problems in your code:
rep.getBytes()writes the bytes to an existing buffer which must have been allocated with the required size. Your buffer created withvar buffer: UInt8 = UInt8()has the size zero.rep.getBytes()is the number of bytes written to the buffer, not a pointer to the data.The easiest way would be to create an
NSMutableDataobject with the required size and pass its mutable data pointer torep.getBytes():Note also that the ALAssetsLibrary framework is deprecated as of iOS 9. If your minimum deployment target is iOS 8 or later then you can achieve the same with
PHAssetandPHImageManagerfrom the Photos framework: