现在的位置: 首页 > 综合 > 正文

保存视频到iOS模拟器

2018年01月23日 ⁄ 综合 ⁄ 共 2176字 ⁄ 字号 评论关闭

    在项目中需要把视屏拷到模拟器上去,百度了下,解决了这个问题。在此对这个问题总结下:

    有两种方法:

    ①使用UIVideoAtPathIsCompatibleWithSavedPhotosAlbum函数,代码如下:

    NSString * path=[[NSBundle mainBundle] pathForResource:@"Miniature" ofType:@"mp4"];

    NSLog(@"path为%@",path);
    
    //**************************************************************************************
    //第①种方法
    //如果当把视频文件拖动到项目中,在TARGETS->Build Phases->Copy Bundle Resource中如果没有视频文件,
    //会导致path为NULL,则保存视频失败!
    
    if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path))
	{
		UISaveVideoAtPathToSavedPhotosAlbum(path,nil,nil,nil);
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"✅Success!视频已添加到PhotosAlbum!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        NSLog(@"✅Success!视频已添加到PhotosAlbum!");
	}
	else {
		UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"❌Failure!视频没有添加到PhotosAlbum!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
        NSLog(@"❌Failure!视频没有添加到PhotosAlbum!");
	}

    ②使用ALAssetsLibrary类的writeVideoAtPathToSavedPhotosAlbum: completionBlock:方法。

    当然要使用ALAssetsLibrary类,要在项目中添加AssetsLibrary.framwork,并包含其头文件:“#import <AssetsLibrary/AssetsLibrary.h>”。

    具体代码如下:

    NSString * path=[[NSBundle mainBundle] pathForResource:@"Miniature" ofType:@"mp4"];

    NSLog(@"path为%@",path);
    
    //**************************************************************************************
    //第②种方法
    //ALAssetsLibrary类是代表系统中整个资源库,使用它可以访问资源库中的资源和保存照片,视频等功能。
    
    //如果当把视频文件拖动到项目中,在TARGETS->Build Phases->Copy Bundle Resource中如果没有视频文件,
    //会导致path为NULL,则保存视频失败!
    
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {
                                    if (error) {
                                        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"❌Failure!视频没有添加到PhotosAlbum!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                        [alert show];
                                        NSLog(@"❌Failure!视频没有添加到PhotosAlbum!");
                                    } else {
                                        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"✅Success!视频已添加到PhotosAlbum!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                                        [alert show];
                                        NSLog(@"✅Success!视频已添加到PhotosAlbum!");
                                    }
                                }];
}

    上面的Miniature.mp4为要添加到模拟器的视频。两种方法任选一种,运行截图如下:

    上面若有不正确之处,还请大家多指点。

   ( 项目代码地址:http://download.csdn.net/detail/little_virus/7912587

   

抱歉!评论已关闭.