裁剪AVAssetvideo与AVFoundation不工作的iOS 8

这一直在困扰着我的最后一天,我曾经在ObjC中使用这种方法将video裁剪成方块,这似乎是我在几年内发现的唯一方法,但在最近试图裁剪后使用它Swift和iOS 8似乎并没有裁剪video,希望有人可以帮忙?

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) { if error != nil { println("Error Outputting recording") } else { self.writeVideoToAssetsLibrary(self.outputUrl!.copy() as NSURL) } } func writeVideoToAssetsLibrary(videoUrl: NSURL) { var videoAsset: AVAsset = AVAsset.assetWithURL(videoUrl) as AVAsset var clipVideoTrack = videoAsset.tracksWithMediaType(AVMediaTypeVideo).first as AVAssetTrack var composition = AVMutableComposition() composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: CMPersistentTrackID()) var videoComposition = AVMutableVideoComposition() videoComposition.renderSize = CGSizeMake(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height) videoComposition.frameDuration = CMTimeMake(1, 30) var transformer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack) var instruction = AVMutableVideoCompositionInstruction() instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30)) var transform1: CGAffineTransform = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, (clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) / 2) var transform2 = CGAffineTransformRotate(transform1, CGFloat(M_PI_2)) var finalTransform = transform2 transformer.setTransform(finalTransform, atTime: kCMTimeZero) instruction.layerInstructions = [transformer] videoComposition.instructions = [instruction] var exporter = AVAssetExportSession(asset: videoAsset, presetName: AVAssetExportPresetHighestQuality) exporter.videoComposition = videoComposition exporter.outputURL = videoUrl exporter.outputFileType = AVFileTypeQuickTimeMovie exporter.exportAsynchronouslyWithCompletionHandler({ () -> Void in dispatch_async(dispatch_get_main_queue(), { self.handleExportCompletion(exporter) }) }) } func handleExportCompletion(session: AVAssetExportSession) { var library = ALAssetsLibrary() if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(session.outputURL) { var completionBlock: ALAssetsLibraryWriteVideoCompletionBlock completionBlock = { assetUrl, error in if error != nil { println("error writing to disk") } else { } } library.writeVideoAtPathToSavedPhotosAlbum(outputUrl, completionBlock: completionBlock) } } 

所以事实certificate,如果您尝试将导出器的outputUrl设置为与您所做的编辑相同的资源,则不会对其进行编辑,所以非常愚蠢! 所以为了将来的引用,outputUrl应该被设置为一个新的唯一的

我不能给你确切的答案,因为我使用C#(Xamarin)进行iOS编程。 但我使用setCropRectangle裁剪video。 在C#中,它看起来像这样:

 layerInstruction.SetCrop (new CGRect (_cropMove, 0, _width / 2, height), transformTime); 

看看文档: setCropRectangle