iOS 相冊(cè),沙盒資源屬性獲取

我想說這是一篇很全的視頻處理整理寶貝們赤炒,燥熱起來吧氯析。
技術(shù)探索一步一步來,怎么發(fā)現(xiàn)的又是怎么處理的莺褒,繞了多大一個(gè)彎道掩缓,彎道中學(xué)了多少知識(shí)。后續(xù)講一些分片上傳的東西

公司最近要做視頻拍攝及上傳遵岩。
技術(shù)摸索你辣,本以為應(yīng)用拍攝后的數(shù)據(jù)都是保存在相冊(cè)巡通,那么我的第一個(gè)思路就是獲取相冊(cè)資源并解讀其中的數(shù)據(jù)。

【相冊(cè)資源】

值得注意的一個(gè)框架AssetsLibrary.framework這個(gè)framework專門處理相冊(cè)數(shù)據(jù)的

  • ALAssetsLibrary:相冊(cè)庫
  • ALAssetsGroup:分類組
  • ALAsset:每個(gè)元素
index.png

上圖是3者互存關(guān)系舍哄,下面上代碼宴凉。

首先是獲取所有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:@"訪問相冊(cè)失敗" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil];
                [alertView show];
            }];
        });
    }
    return _groups;
}

然后對(duì)ALAssetsGroup進(jìn)行分類展示,自定義GCMAssetModel表悬,這個(gè)類用于保存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(@"相冊(cè)圖片訪問失敗");
        }];
        
    }
}

【沙盒資源】

之后老板的需求是要通過app拍攝然后保存到本地(也就是沙盒),當(dāng)時(shí)很蒙圈蟆沫,開始查找哪個(gè)函數(shù)能指定存儲(chǔ)位置籽暇,然而我沒有查到,我就自己摸索其他方案饥追,我就每次用我做的app拍攝視頻發(fā)現(xiàn)每次拍攝后我都發(fā)現(xiàn)在tmp中都有臨時(shí)存儲(chǔ)的mov文件图仓。將包下載下來后播放發(fā)現(xiàn)正是我們想要的罐盔。

視頻臨時(shí)存儲(chǔ)位置.png

知道了這個(gè)機(jī)制我就想通過url能否獲取視頻內(nèi)部信息呢但绕,然后我就找到了這個(gè)方法:

-(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(@"總時(shí)間:%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妨蛹,這樣數(shù)據(jù)就全了症副。
推薦幾個(gè)文章。
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://www.reibang.com/p/931f8e85dc37

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末椿浓,一起剝皮案震驚了整個(gè)濱河市纬黎,隨后出現(xiàn)的幾起案子幅骄,更是在濱河造成了極大的恐慌,老刑警劉巖本今,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件拆座,死亡現(xiàn)場離奇詭異,居然都是意外死亡冠息,警方通過查閱死者的電腦和手機(jī)挪凑,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來逛艰,“玉大人躏碳,你說我怎么就攤上這事∩⒉溃” “怎么了菇绵?”我有些...
    開封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長镇眷。 經(jīng)常有香客問我咬最,道長,這世上最難降的妖魔是什么欠动? 我笑而不...
    開封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任丹诀,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘铆遭。我一直安慰自己硝桩,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開白布枚荣。 她就那樣靜靜地躺著碗脊,像睡著了一般。 火紅的嫁衣襯著肌膚如雪橄妆。 梳的紋絲不亂的頭發(fā)上衙伶,一...
    開封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音害碾,去河邊找鬼矢劲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛慌随,可吹牛的內(nèi)容都是我干的芬沉。 我是一名探鬼主播,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼阁猜,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼丸逸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起剃袍,我...
    開封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤黄刚,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后民效,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體憔维,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年畏邢,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了业扒。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡棵红,死狀恐怖凶赁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情逆甜,我是刑警寧澤虱肄,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站交煞,受9級(jí)特大地震影響咏窿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜素征,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一集嵌、第九天 我趴在偏房一處隱蔽的房頂上張望萝挤。 院中可真熱鬧,春花似錦根欧、人聲如沸怜珍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽酥泛。三九已至,卻和暖如春嫌拣,著一層夾襖步出監(jiān)牢的瞬間柔袁,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來泰國打工异逐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留捶索,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓灰瞻,卻偏偏與公主長得像腥例,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子箩祥,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容