gpt4 book ai didi

ios - 下载视频并将其保存到相机胶卷

转载 作者:可可西里 更新时间:2023-11-01 00:52:51 34 4
gpt4 key购买 nike

根据我阅读的所有示例和文档,这似乎应该是非常简单的事情,但由于某些奇怪的原因我仍然无法让它工作。

我正在使用 Alamofire Frame 工作从 Instagram 下载视频。下载后,我想将视频保存到相机胶卷。这是我下载视频并保存到磁盘的代码:

let destination: (NSURL, NSHTTPURLResponse) -> (NSURL) = {
(temporaryURL, response) in

if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {

let finalPath = directoryURL.URLByAppendingPathComponent("\(Scripts.dateToString2(date: NSDate())).\(response.suggestedFilename!)")
InstagramEngine.downloadMediaPath = finalPath

Scripts.log("Final Path >> \(finalPath)")

return finalPath
}

return temporaryURL
}

let request = Alamofire.download(.GET, self.videoURL, destination)

request.response { _, response, data, error in

NSNotificationCenter.defaultCenter().postNotificationName(kMediaDownloadComplete, object: nil)

}

一旦下载完成,就会触发调用此函数将其保存到相机胶卷的通知:

UISaveVideoAtPathToSavedPhotosAlbum(InstagramEngine.downloadMediaPath.URLString, self, Selector("video:didFinishSavingWithError:contextInfo:"), nil)

一切都是根据我的日志语句调用的,没有发生错误。甚至为 UISaveVideoAtPathToSavedPhotosAlbum 成功调用了 didFinishSavingWithError,我确认没有发现错误。但是当我去查看相机胶卷时,我仍然没有看到那里保存的视频。有什么想法吗?

最佳答案

不幸的是,存在与 UISaveVideoAtPathToSavedPhotosAlbummp4 格式相关的错误,这是 Instagram 使用的格式。

有一个名为 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum 的辅助方法可以帮助指示视频是否与方法 UISaveVideoAtPathToSavedPhotosAlbum 兼容。对于从 Instagram 下载的视频,这将返回 false


幸运的是,仍然可以将视频存储到相机胶卷中。这可以使用 ALAssetsLibrary。我已尝试获取您的示例代码并对其进行调整以使用 ALAssetsLibrary,希望这可以帮助您使其正常工作。

import AssetsLibrary

...
...

func downloadVideoToCameraRoll() {

// Local variable pointing to the local file path for the downloaded video
var localFileUrl: String?

// A closure for generating the local file path for the downloaded video. This will be pointing to the Documents directory with a unique UDID file name.
let destination: (NSURL, NSHTTPURLResponse) -> (NSURL) = {
(temporaryURL, response) in

if let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
let finalPath = directoryURL.URLByAppendingPathComponent("\(NSUUID()).\(response.suggestedFilename!)")
localFileUrl = finalPath.absoluteString
return finalPath
}

return temporaryURL
}

// The media post which should be downloaded
let postURL = NSURL(string: "https://api.instagram.com/v1/media/" + "952201134785549382_250131908" + "?access_token=" + InstagramEngine.sharedEngine().accessToken)!

// Then some magic happens that turns the postURL into the videoURL, which is the actual url of the video media:
let videoURL = NSURL(string: "https://scontent.cdninstagram.com/hphotos-xfp1/t50.2886-16/11104555_1603400416544760_416259564_s.mp4")!

// Download starts
let request = Alamofire.download(.GET, videoURL, destination)

// Completion handler for the download
request.response { (request, response, data, error) -> Void in
if let path = localFileUrl {
let isVideoCompatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path)
println("bool: \(isVideoCompatible)") // This logs out "bool: false"

let library = ALAssetsLibrary()

library.writeVideoAtPathToSavedPhotosAlbum(NSURL(string: path), completionBlock: { (url, error) -> Void in
// Done! Go check your camera roll
})
}
}
}

关于ios - 下载视频并将其保存到相机胶卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31285850/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com