iOS---如何獲取手機中的視頻和照片

最近的項目中需要獲取手機里的本地視頻,找到相關(guān)資料比較麻煩,因此在此做分享

@interface ViewController ()
@property (nonatomic,strong) NSMutableArray        *groupArrays;
@property (nonatomic,strong) UIImageView           *litimgView;
@end```
在本例中我僅只用了一個uiimageview去獲取一個視頻的縮略圖,各位如有需要可進行擴展
首先在viewdidload中

    self.navigationItem.title = @"Demo";
    self.view.backgroundColor = [UIColor clearColor];
    
    // 初始化
    self.groupArrays = [NSMutableArray array];
    
    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(50, 50, 100, 50);
    btn1.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn1];
    [btn1 addTarget:self action:@selector(testRun) forControlEvents:UIControlEventTouchUpInside];
    // 圖片或者視頻的縮略圖顯示
    self.litimgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 120, 120)];
    [self.view addSubview:_litimgView];

再然后獲取視頻和照片

-(void)run
{
__weak ViewController *weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
if (group != nil) {
[weakSelf.groupArrays addObject:group];
} else {
[weakSelf.groupArrays enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[obj enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if ([result thumbnail] != nil) {
// 照片
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]){

// NSDate *date= [result valueForProperty:ALAssetPropertyDate];
// UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];
// NSString *fileName = [[result defaultRepresentation] filename];
// NSURL *url = [[result defaultRepresentation] url];
// int64_t fileSize = [[result defaultRepresentation] size];
//
// NSLog(@"date = %@",date);
// NSLog(@"fileName = %@",fileName);
// NSLog(@"url = %@",url);
// NSLog(@"fileSize = %lld",fileSize);
//
// // UI的更新記得放在主線程,要不然等子線程排隊過來都不知道什么年代了,會很慢的
// dispatch_async(dispatch_get_main_queue(), ^{
// self.litimgView.image = image;
// });
NSLog(@"讀取到照片了");
}
// 視頻
else if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo] ){

                            NSURL *url = [[result defaultRepresentation] url];
                             UIImage *image = [UIImage imageWithCGImage:[result thumbnail]];                                NSLog(@"%@",url);
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    self.litimgView.image = image;
                                });
                            // 和圖片方法類似
                        }
                    }
                }];
            }];
            
        }
    };
    
    ALAssetsLibraryAccessFailureBlock failureBlock = ^(NSError *error)
    {
        
        NSString *errorMessage = nil;
        
        switch ([error code]) {
            case ALAssetsLibraryAccessUserDeniedError:
            case ALAssetsLibraryAccessGloballyDeniedError:
                errorMessage = @"用戶拒絕訪問相冊,請在<隱私>中開啟";
                break;
                
            default:
                errorMessage = @"Reason unknown.";
                break;
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"錯誤,無法訪問!"
                                                               message:errorMessage
                                                              delegate:self
                                                     cancelButtonTitle:@"確定"
                                                     otherButtonTitles:nil, nil];
            [alertView show];
        });
    };
    
    
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]  init];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll
                                 usingBlock:listGroupBlock failureBlock:failureBlock];
});

}

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容