在开发用到视频时,模拟器中往往没有视频可用,下面的方法可以把工程中得视频导入到模拟器中
- (void)saveAction
{
NSMutableArray *videoArray = [NSMutableArray arrayWithCapacity:3];
NSArray *movs = [[NSBundle mainBundle] pathsForResourcesOfType:@"mov" inDirectory:nil];
[videoArray addObjectsFromArray:movs];
// save video to camera roll
for (id item in videoArray) {
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(item)) {
// Note:save to camera roll is async, so the later item may copy complete than previous item
UISaveVideoAtPathToSavedPhotosAlbum(item, self, nil, NULL);
}
}
}
#import
- (void)addMovie
{
UIImagePickerController *moviePicker = [[UIImagePickerController alloc] init];
moviePicker.delegate = self;
moviePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
moviePicker.allowsEditing = YES;
moviePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
moviePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
} else {
moviePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentViewController:moviePicker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取图片的NSURL 来源于AssetsLibrary.framework #import
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
//ALAssetsLibrary 获取图片和视频
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc]init];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (url) {
// 主要方法
[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc((unsigned long)rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:((unsigned long)rep.size) error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
NSString * videoPath = @"沙盒地址";
[data writeToFile:videoPath atomically:YES];
} failureBlock:nil];
}
});
[picker dismissViewControllerAnimated:YES completion:NULL];
}
获取视频缩略图
+ (UIImage *)getVideoPreViewImage:(NSURL *)videoPath
{
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoPath options:nil];
AVAssetImageGenerator *gen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
gen.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0, 10);
NSError *error = nil;
CMTime actualTime;
CGImageRef image = [gen copyCGImageAtTime:time actualTime:&actualTime error:&error];
UIImage *img = [[UIImage alloc] initWithCGImage:image];
CGImageRelease(image);
return img;
}