iOS模拟器中导入视频,获取相册视频,视频缩略图

本文介绍了在iOS应用开发中如何将工程中的视频导入到模拟器中,使用UIImagePickerController选取相册视频,并通过ALAssetsLibrary获取视频数据。此外,还提供了一个方法用于生成视频的预览图像。通过这些步骤,开发者可以在模拟器中方便地处理和展示视频资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发用到视频时,模拟器中往往没有视频可用,下面的方法可以把工程中得视频导入到模拟器

- (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 <AssetsLibrary/AssetsLibrary.h>

- (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 <AssetsLibrary/AssetsLibrary.h>

    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;

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值