zoukankan      html  css  js  c++  java
  • iOS如何获取手机的本地照片和相册

        __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];
        });
    
  • 相关阅读:
    session
    Cookie
    HttpServletRequest
    HttpServletResponse response(中文乱码、文件下载、定时刷新、控制缓存、重定向、注意事项)
    采购价格
    SAP 会计凭证
    STO 后台配置
    SAP 公司间采购
    SAP Dependency 相关性
    SAP 委外加工
  • 原文地址:https://www.cnblogs.com/zhendiao/p/5128608.html
Copyright © 2011-2022 走看看