【问题标题】:How to add video from camera roll to app on Xcode (Swift) [closed]如何将相机胶卷中的视频添加到 Xcode(Swift)上的应用程序 [关闭]
【发布时间】:2017-05-06 00:56:59
【问题描述】:

我希望用户能够从他们的相机胶卷中导入视频,对其进行编辑,然后将视频保存到他们的相机胶卷中。我该怎么做?

【问题讨论】:

    标签: ios swift xcode video camera


    【解决方案1】:

    嗯,首先你需要像这样询问用户的权限:

    AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { response in
        if response {
            //access granted
        } else {
    
        }
    }
    
    //Photos
    let photos = PHPhotoLibrary.authorizationStatus()
    if photos == .notDetermined {
        PHPhotoLibrary.requestAuthorization({status in
            if status == .authorized{
                ...
            } else {}
        })
    }
    

    然后你需要在 plist 文件中添加一些关于你为什么想要它的描述,如下所示:

    <key>NSCameraUsageDescription</key>
    <string>You can take photos</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>You can select photos to attach it on this app.</string>
    

    关于剪辑视频,这是一个非常主观的话题。你需要做什么样的改变?而且取决于你想要做什么,它可能会非常复杂。

    稍后保存到相机胶卷,我得到了一个下载文件并将其保存到相机胶卷的代码示例:

    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
            })
        }
    }
    }
    

    祝你好运!

    【讨论】:

    • 当我将它添加到我的代码时,它显示“预期声明”,为什么会出现此错误? :(
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多