Reputation: 1885
Im creating a simple app to upload a recorded video to a web API. After recording the video in app I can successfully play the video within the app, but I'm struggling to access the video to upload it.
The video is at assets-library://asset/asset.MOV?id=3AFCEC9B-17DE-4D75-9B87-0AD50BAB9BFF&ext=MOV
, which can be loaded using MPMoviePlayerController(contentURL: url)
so I know it exists there.
I've tried the following few methods with no success:
Method 1
let url = NSURL(fileURLWithPath: thisNote.url!)
println("This url = \(thisNote.url)")
let videoData = NSData(contentsOfURL: url!, options: nil, error: &e)
error = The operation couldn’t be completed. No such file or directory
Method 2
let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docsDir = dirPaths[0] as! String
let videoFilePath = docsDir.stringByAppendingPathComponent(Constants.Directories.appFolder)
let filePath = NSBundle.pathForResource(thisNote.fileName, ofType: "video/quicktime", inDirectory: Constants.Directories.appFolder)
filepath = nil
Method 3
var video:NSData = NSData()
ALAssetsLibrary().assetForURL(url, resultBlock: { (asset : ALAsset!) -> Void in
if let rep : ALAssetRepresentation = asset.defaultRepresentation(){
var error: NSError?
let length = Int(rep.size())
let from = Int64(0)
let data = NSMutableData(length: length)!
let numRead = rep.getBytes(UnsafeMutablePointer(data.mutableBytes), fromOffset: from, length: length, error: &error)
video = data
self.uploadNote(video, note: thisNote)
}
}){ (error : NSError!) -> Void in
println("Asset library error: \(error)")
}
The result block is not reached, nor is the error printed.
Any help on this would be appreciated.
Upvotes: 3
Views: 1859
Reputation: 1885
Method three was the closest to the final solution. I ended up enumerating through the asset library to find the right video file. Whilst I'm sure this is not the most efficient way to get the video, it works. Here is the code I used, I hope it helps someone......
let url = NSURL(string: thisNote.url!)
var video:NSData = NSData()
AssetLibraryHelper.sharedInstance.assetForURL(url, resultBlock: { (foundAsset : ALAsset!) -> Void in
var asset:ALAsset?
if foundAsset == nil {
// Workaround: Enumerate over the asset library to find matching URL
AssetLibraryHelper.sharedInstance.enumerateGroupsWithTypes(
ALAssetsGroupType(ALAssetsGroupPhotoStream),
usingBlock: { (group: ALAssetsGroup?, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
if group != nil {
group!.enumerateAssetsWithOptions(NSEnumerationOptions.Reverse, usingBlock: {(enumeratedAsset: ALAsset!, index: Int, stopGroup: UnsafeMutablePointer<ObjCBool>) -> Void in
if enumeratedAsset != nil {
asset = enumeratedAsset
stop.initialize(true)
stopGroup.initialize(true)
}
})
}
}, failureBlock: { (error: NSError!) -> Void in
print("Error enumerating assets \(error)")
})
}
else {
asset = foundAsset
}
print("Asset is \(asset)")
if let rep : ALAssetRepresentation = asset!.defaultRepresentation(){
let image = UIImage(CGImage: rep.fullResolutionImage().takeUnretainedValue())
let imageData = UIImageJPEGRepresentation(image, 0.7)
var error: NSError?
let length = Int(rep.size())
let from = Int64(0)
let data = NSMutableData(length: length)!
let _ = rep.getBytes(UnsafeMutablePointer(data.mutableBytes), fromOffset: from, length: length, error: &error)
video = data
self.uploadNote(video, note: thisNote, imageFile: imageData!)
}
}){ (error : NSError!) -> Void in
print("Asset library error: \(error)")
}
Upvotes: 1