9

私はそれを理解しようとしていますが、有用な情報を見つけることができません。私はこれだけを見つけました:

PHAssetResourceManager.defaultManager().writeDataForAssetResource(assetRes, 
toFile: fileURL, options: nil, completionHandler: 
{
     // Video file has been written to path specified via fileURL
}

しかし、私はそれをどのようにプレイするか分からないことを恥ずかしく思います. UIImagePickerController を作成し、カメラ ロールからイメージをロードしました。

4

5 に答える 5

0

スイフト5

func videoUrlForLivePhotoAsset(asset: PHAsset, completionHandler: @escaping (_ result: URL?) -> Void) {
            
    print("videoUrlForLivePhotoAsset: \(asset)")
    
    let options : PHLivePhotoRequestOptions = PHLivePhotoRequestOptions.init()
    
    options.deliveryMode = .fastFormat
    options.isNetworkAccessAllowed = true
    
    PHImageManager.default().requestLivePhoto(for: asset, targetSize: UIScreen.main.bounds.size, contentMode: .default, options: options) { (livePhoto, info) in
        
        if livePhoto != nil {
            
            let assetResources : [PHAssetResource] = PHAssetResource.assetResources(for: livePhoto!)
            
            var videoResource : PHAssetResource?
            
            for resource in assetResources {
                
                if resource.type == .pairedVideo {
                    
                    videoResource = resource

                    break
                    
                }
                
            }
            
            guard let photoDir = self.generateFolderForLivePhotoResources() else {

                return

            }

            
            print("videoResource: \(videoResource)")
            
            if videoResource != nil {
                
                self.saveAssetResource(resource: videoResource!, inDirectory: photoDir, buffer: nil, maybeError: nil) { (fileUrl) in
                    
                    completionHandler(fileUrl)

                }
                                                            
            }
            
        } else {
            
            completionHandler(nil)

        }
        
    }
    
}

func saveAssetResource(
    resource: PHAssetResource,
    inDirectory: NSURL,
    buffer: NSMutableData?, maybeError: Error?, completionHandler: @escaping (_ result: URL?) -> Void) {
    
    guard maybeError == nil else {
        print("Could not request data for resource: \(resource), error: \(String(describing: maybeError))")
        return
    }

    let maybeExt = UTTypeCopyPreferredTagWithClass(
        resource.uniformTypeIdentifier as CFString,
        kUTTagClassFilenameExtension
        )?.takeRetainedValue()

    guard let ext = maybeExt else {
        return
    }

    guard var fileUrl = inDirectory.appendingPathComponent(NSUUID().uuidString) else {
        print("file url error")
        return
    }

    fileUrl = fileUrl.appendingPathExtension(ext as String)

    if let buffer = buffer, buffer.write(to: fileUrl, atomically: true) {
        
        print("Saved resource form buffer \(resource) to filepath \(String(describing: fileUrl))")

        completionHandler(fileUrl)
        
    } else {

        PHAssetResourceManager.default().writeData(for: resource, toFile: fileUrl, options: nil) { (error) in

            print("Saved resource directly \(resource) to filepath \(String(describing: fileUrl))")

            if error == nil {
                
                completionHandler(fileUrl)

            } else {

                completionHandler(nil)

            }

        }

    }
    
}

func generateFolderForLivePhotoResources() -> NSURL? {
    
    let photoDir = NSURL(
        // NB: Files in NSTemporaryDirectory() are automatically cleaned up by the OS
        fileURLWithPath: NSTemporaryDirectory(),
        isDirectory: true
        ).appendingPathComponent(NSUUID().uuidString)

    let fileManager = FileManager()

    // we need to specify type as ()? as otherwise the compiler generates a warning

    let success : ()? = try? fileManager.createDirectory(
        at: photoDir!,
        withIntermediateDirectories: true,
        attributes: nil
    )

    return success != nil ? photoDir! as NSURL : nil
    
}

次のように呼び出します。

let asset = PHAsset.init()
                    
self.videoUrlForLivePhotoAsset(asset: asset!) { (url) in
                        
    print("url: \(url)")

}

注: Temp および Documents ディレクトリをクリーンアップし、ファイルを削除する必要があります。

于 2021-04-25T23:14:13.037 に答える