我创建了一个共享扩展,其目的是将视频共享到我的容器应用程序。作为共享流程的一部分,我想检查用户是否登录等。所以我创建了一个应用组并在容器应用和扩展之间共享数据。(同时使用 NSUserDefaults 和核心数据)
let sharedUserDefaults = NSUserDefaults(suiteName:"group.app.group.name")
lazy var applicationDocumentsDirectory: NSURL = {
NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.app.group.name")!
}()
这工作正常。
现在,当我尝试使用压缩视频时SDAVAssetExportSession
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH-mm-ss"
let timeStamp = NSDate()
let asset = AVAsset(URL: self.shareVideoURL)
let exportSession = SDAVAssetExportSession(asset: asset)
let outputURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("\(dateFormatter.stringFromDate(timeStamp)).MP4")
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileTypeMPEG4
exportSession.shouldOptimizeForNetworkUse = true
let assetTrack = asset.tracksWithMediaType(AVMediaTypeVideo).first!
let videoWidth:NSNumber = assetTrack.naturalSize.width, videoHeight:NSNumber = assetTrack.naturalSize.height,
let desiredBitrate:NSNumber = 3*1024*1024 , desiredKeyFrameInterval:NSNumber = 20
exportSession.videoSettings = [AVVideoCodecKey:AVVideoCodecH264,
AVVideoWidthKey:videoWidth,
AVVideoHeightKey:videoHeight,
AVVideoCompressionPropertiesKey: [
AVVideoAverageBitRateKey:desiredBitrate,
AVVideoMaxKeyFrameIntervalKey:desiredKeyFrameInterval]]
exportSession.audioSettings = [AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100,
AVEncoderBitRateKey: 64000]
exportSession.exportAsynchronouslyWithCompletionHandler({
switch exportSession.status {
case .Completed:
print("Compressed Video size : \(Double(NSData(contentsOfURL: outputURL)!.length)/(1024*1024)) mb")
case .Failed:
print("Export Failed :\(exportSession.error)")
case .Cancelled:
print("Export Cancelled :\(exportSession.error)")
default:
print("complete")
}
})
当我在共享扩展中执行此操作时,出现错误:
Error Domain=NSURLErrorDomain Code=-3000 "Cannot create file"
UserInfo={NSLocalizedDescription=Cannot create file, NSUnderlyingError=0x160900450
{Error Domain=NSOSStatusErrorDomain Code=-12149 "(null)"}}
当我使用文档目录在另一个应用程序中运行此代码时,它可以正常工作,没有任何问题,但是当我开始使用应用程序组容器时,它不允许保存文件。
谁能指出我正确的方向将压缩文件保存在共享应用程序组容器中