【问题标题】:Download video and save in custom album in library?下载视频并保存在库中的自定义相册中?
【发布时间】:2014-06-24 11:15:18
【问题描述】:

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

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];

    }

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

【问题讨论】:

    标签: objective-c xcode video download


    【解决方案1】:

    您可以像这样使用“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 可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-07-05
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多