下载视频并保存在图书馆的自定义相册中?

时间:2014-06-24 11:15:19

标签: objective-c xcode video download

我使用此代码在我的照片库中创建了一个相册

NSString *albumName=@"iphonemaclover";
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library addAssetsGroupAlbumWithName:albumName
                             resultBlock:^(ALAssetsGroup *group) {
                                 NSLog(@"added album:%@", albumName);

我已使用此代码从服务器下载视频

-(IBAction)btnDownload:(UIView *)sender {


    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"DOWNLOADING..."];

    NSString *albumName=@"album name";



                                 NSURL *url = [NSURL URLWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"];





                                 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];



                                 [request setDelegate:self];
                                 [request setDownloadDestinationPath:@"/Users/seemtech/Desktop/dd/vvv.m4v"];//work fine 
                                 [ASIHTTPRequest setDefaultTimeOutSeconds:3000];

                                 [request startAsynchronous];



                             }
                            failureBlock:^(NSError *error) {
                                NSLog(@"error adding album");
                            }];


            }

    -(void)requestFinished:(ASIHTTPRequest *)request

    {

    [[[[UIAlertView alloc] initWithTitle:@"Message"
                                 message:@"Success!!!"
                                delegate:self
                       cancelButtonTitle:@"OK"
                       otherButtonTitles:nil] autorelease] show]; [DSBezelActivityView removeViewAnimated:YES];


    }

    -(void)requestFailed:(ASIHTTPRequest *)request {

    NSLog(@"error==%@",request.error); [DSBezelActivityView removeViewAnimated:YES];

    }

我需要将视频保存在我创建的相册中。 我该怎么做?

1 个答案:

答案 0 :(得分:2)

您可以使用“ALAssetsLibrary”将视频保存到已保存的相册:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *capturedVideoURL = [NSURL URLWithString:@"/Users/seemtech/Desktop/dd/vvv.m4v"];

if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:capturedVideoURL]) {
    // request to save video in photo roll.
    [library writeVideoAtPathToSavedPhotosAlbum:capturedVideoURL completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error) {
            NSLog(@"error while saving video");
        } else{
            [self addAssetURL:assetURL toAlbum:@"iphonemaclover"];
        }
    }];
}

将该资源添加到您的相册

- (void)addAssetURL:(NSURL*)assetURL toAlbum:(NSString*)albumName
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
            //If album found
            [library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
            //add asset to album
                [group addAsset:asset];
            } failureBlock:nil];
        }
        else {
            //if album not found create an album
            [library addAssetsGroupAlbumWithName:albumName resultBlock:^(ALAssetsGroup *group)     {
                [self addAssetURL:assetURL toAlbum:albumName];
            } failureBlock:nil];
        }
    } failureBlock: nil];
}

此代码的唯一问题是它会保存已保存相册中的视频副本。因此,找到一种从相册中删除资产的方法。这Link可能会有所帮助。