记录一下ShareExtention一个视频分享的功能。 需要用到appgroups

[itemProvider loadItemForTypeIdentifier:@"public.movie" options:nil completionHandler:^(id _Nullable item, NSError * _Null_unspecified error) {
// 对itemProvider夹带着的图片进行解析
NSURL *videoUrl = (NSURL *)item;
NSString *videoName = [videoUrl lastPathComponent];
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:[NSString stringWithFormat:@"group.%@",bundleIdentifier]];
NSURL *uotPutPath;
NSString *outputStr = [NSString stringWithFormat:@"%@/%.0f%d.mp4",self.storagePath,[[NSDate date] timeIntervalSince1970],arc4random()%100];
if ([outputStr hasPrefix:@"file://"]) {
uotPutPath = [NSURL URLWithString:outputStr];
} else {
uotPutPath = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",outputStr]];
}
AVURLAsset *anAsset = [[AVURLAsset alloc]initWithURL:videoUrl options:nil];
// AVURLAsset *anAsset = (AVURLAsset *)exportSession.asset;
SDAVAssetExportSession *encoder = [SDAVAssetExportSession.alloc initWithAsset:anAsset];
encoder.outputFileType = @"com.apple.quicktime-movie";
encoder.outputURL = uotPutPath;
encoder.videoSettings = @{
AVVideoCodecKey: AVVideoCodecH264,
AVVideoWidthKey: @(self.view.frame.size.width),
AVVideoHeightKey: @(self.view.frame.size.height),
AVVideoCompressionPropertiesKey: @
{
AVVideoAverageBitRateKey: @(2*self.view.frame.size.width*self.view.frame.size.height),
AVVideoProfileLevelKey: AVVideoProfileLevelH264MainAutoLevel,
},
};
[encoder exportAsynchronouslyWithCompletionHandler:^
{
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicatorView stopAnimating];
NSLog(@"00============%f", encoder.progress);
if (encoder.status == AVAssetExportSessionStatusCompleted)
{
NSLog(@"Video export succeeded");
NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
[dataDic setObject:[NSString stringWithFormat:@"%@",uotPutPath] forKey:@"imageUrl"];
[dataDic setObject:uotPutPath.absoluteString forKey:@"videoURL"];
[self.imageDataArray addObject:dataDic];
//存图片数组
[userDefaults setObject:self.imageDataArray forKey:@"shareImageDataArray"];
//用于标记是新的分享
[userDefaults setBool:YES forKey:@"newShare"];
//获取全部再销毁
[self.extensionContext completeRequestReturningItems:@[] completionHandler:nil];
NSURL *destinationURL = [NSURL URLWithString:[NSString stringWithFormat:@"dachenmedicalcircle://%@",@"saveFilePath"]];
NSString *className = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned char []){0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E} length:13] encoding:NSASCIIStringEncoding];
if (NSClassFromString(className)) {
id object = [NSClassFromString(className) performSelector:@selector(sharedApplication)];
[object performSelector:@selector(openURL:) withObject:destinationURL];
}
}
else if (encoder.status == AVAssetExportSessionStatusCancelled)
{
NSLog(@"Video export cancelled");
}
else
{
NSLog(@"Video export failed with error: %@ (%ld)", encoder.error.localizedDescription, (long)encoder.error.code);
}
});
}];
}]
- (NSString *)storagePath
{ if (_storagePath) { return _storagePath; } NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; NSURL *groupURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[NSString stringWithFormat:@"group.%@",bundleIdentifier]]; NSString *groupPath = [groupURL path]; _storagePath = [groupPath stringByAppendingPathComponent:APP_FOLDER_NAME]; NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL success = [fileManager removeItemAtPath:_storagePath error:&error];
if (success) {
NSLog(@"删除共享文件夹");
}
if (![fileManager fileExistsAtPath:_storagePath]) {
[fileManager createDirectoryAtPath:_storagePath withIntermediateDirectories:NO attributes:nil error:nil];
}
return _storagePath;
}