iOS 相冊,沙盒資源屬性獲取

我想說這是一篇很全的視頻處理整理寶貝們,燥熱起來吧。
技術探索一步一步來,怎么發(fā)現的又是怎么處理的,繞了多大一個彎道,彎道中學了多少知識。后續(xù)講一些分片上傳的東西

公司最近要做視頻拍攝及上傳。
技術摸索,本以為應用拍攝后的數據都是保存在相冊,那么我的第一個思路就是獲取相冊資源并解讀其中的數據。

【相冊資源】

值得注意的一個框架AssetsLibrary.framework這個framework專門處理相冊數據的

  • ALAssetsLibrary:相冊庫
  • ALAssetsGroup:分類組
  • ALAsset:每個元素
index.png

上圖是3者互存關系,下面上代碼。

首先是獲取所有ALAssetsGroup

@property (nonatomic,strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic,strong) NSMutableArray *groups;

- (ALAssetsLibrary *)assetsLibrary{
    if (_assetsLibrary == nil) {
        _assetsLibrary = [[ALAssetsLibrary alloc] init];
    }
    return _assetsLibrary;
}
- (NSMutableArray *)groups{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
                if(group){
                    [_groups addObject:group];
                    [self.tableView reloadData];
                }
            } failureBlock:^(NSError *error) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"訪問相冊失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
                [alertView show];
            }];
        });
    }
    return _groups;
}

然后對ALAssetsGroup進行分類展示,自定義GCMAssetModel,這個類用于保存assets信息。

- (void)setGroup:(ALAssetsGroup *)group{
    _group = group;
    [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
        if (asset == nil) return ;
        GCMAssetModel *model = [[GCMAssetModel alloc] init];
        if (![[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {//不是圖片
            model.thumbnail = [UIImage imageWithCGImage:asset.thumbnail];
            model.imageURL = asset.defaultRepresentation.url;
            model.isImage = NO;
            [self.assetModels addObject:model];
        }else{
            model.thumbnail = [UIImage imageWithCGImage:asset.thumbnail];
            model.imageURL = asset.defaultRepresentation.url;
            NSLog(@"%@",asset.defaultRepresentation.url);
            model.isImage = YES;
            [self.assetModels addObject:model];
        }
        
    }];
    
}

最后通過ALAssetsLibrary 方法使用assets的url獲取資源信息

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GCMAssetModel *model = self.assetModels[indexPath.item];
    if (model.isImage == NO) {
        
        MPMoviePlayerViewController* playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:model.imageURL];
        [self presentViewController:playerView animated:YES completion:nil];
    }else{
        
        cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        UIVisualEffectView *effectview = [[UIVisualEffectView alloc] initWithEffect:blur];
        effectview.frame = cover.frame;
        [cover addSubview:effectview];
        [self.view addSubview:cover];
        bigImg = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [bigImg addTarget:self action:@selector(removeBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:bigImg];
        
        ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
        [lib assetForURL:model.imageURL resultBlock:^(ALAsset *asset) {
            ALAssetRepresentation *assetRep = [asset defaultRepresentation];
            CGImageRef imgRef = [assetRep fullResolutionImage];
            UIImage *img = [UIImage imageWithCGImage:imgRef
                                               scale:assetRep.scale
                                         orientation:(UIImageOrientation)assetRep.orientation];
            [bigImg setImage:img forState:UIControlStateNormal];
        } failureBlock:^(NSError *error) {
            NSLog(@"相冊圖片訪問失敗");
        }];
        
    }
}

【沙盒資源】

之后老板的需求是要通過app拍攝然后保存到本地(也就是沙盒),當時很蒙圈,開始查找哪個函數能指定存儲位置,然而我沒有查到,我就自己摸索其他方案,我就每次用我做的app拍攝視頻發(fā)現每次拍攝后我都發(fā)現在tmp中都有臨時存儲的mov文件。將包下載下來后播放發(fā)現正是我們想要的。

視頻臨時存儲位置.png

知道了這個機制我就想通過url能否獲取視頻內部信息呢,然后我就找到了這個方法:

-(void)getMovInfoWithAVURLAsset
{
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"1481872850_wm" withExtension:@"MOV"];
    AVURLAsset *movAsset = [AVURLAsset URLAssetWithURL:fileUrl options:nil];
    for (NSString *format in [movAsset availableMetadataFormats]) {
        NSLog(@"formatString: %@",format);
        for (AVMetadataItem *metadataitem in [movAsset metadataForFormat:format]) {
            NSLog(@"commonKey = %@ value = %@",metadataitem.commonKey,metadataitem.value);
            if ([metadataitem.commonKey isEqualToString:@"make"]) {
                NSString *make = (NSString *)metadataitem.value;
                NSLog(@"make: %@",make);
            }
            else if([metadataitem.commonKey isEqualToString:@"software"])
            {
                NSString *software = (NSString *)metadataitem.value;
                NSLog(@"software: %@",software);
            }
            else if([metadataitem.commonKey isEqualToString:@"model"])
            {
                NSString *model = (NSString *)metadataitem.value;
                NSLog(@"model: %@",model);
            }
            else if([metadataitem.commonKey isEqualToString:@"creationDate"])
            {
                NSString *creationDate = (NSString *)metadataitem.value;
                
                NSLog(@"creationDate: %@",creationDate);
            }
        }
    }
    
    CMTime durationTime = movAsset.duration;
    CGFloat duration = CMTimeGetSeconds(durationTime);
    NSLog(@"總時間:%f",duration);
    
    // 視頻截圖
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc]initWithAsset:movAsset];
    generator.appliesPreferredTrackTransform = YES;
    
    CMTime time = CMTimeMakeWithSeconds(0, 30);
    NSValue *timeValue = [NSValue valueWithCMTime:time];
    
    [generator generateCGImagesAsynchronouslyForTimes:@[timeValue] completionHandler:^
     (CMTime requestedTime, CGImageRef image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError *error)
    {
        if (result == AVAssetImageGeneratorSucceeded)
        {
            self.vedioImage.image = [UIImage imageWithCGImage:image];
            // 成功 do something
        }
        else
        {
            // 失敗
        }
    }];
    
}

AVURLAsset 需要AVFoundation.framework,這樣數據就全了。
推薦幾個文章。
http://blog.csdn.net/b719426297/article/details/24312339
http://blog.csdn.net/newjerryj/article/details/7637047
http://blog.csdn.net/u011397277/article/details/52574996
http://m.itdecent.cn/p/931f8e85dc37

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容