ios - 程序流程出错

标签 ios objective-c objective-c-blocks

在这个代码片段中,程序的流程是 先跳出 for 循环,然后才进入 block 内
结果 block :^(ALAsset * Assets )。 该代码首先在底部打印 NSLog,然后执行循环内的 block 。这里发生了什么?

 ALAssetsLibrary *lib=[ALAssetsLibrary new];
    _sizeOfSelectedImage=0;
        for (int i=0; i<assets.count; i++) {
            ALAsset *asset=assets[i];
            FileOP *fileMgr=[[FileOP alloc]init];
            NSString *baseDir=[fileMgr GetDocumentDirectory];

            //STORING FILE INTO LOCAL

            [lib assetForURL:asset.defaultRepresentation.url
                 resultBlock:^(ALAsset *asset){
                     ALAssetRepresentation *repr = [asset defaultRepresentation];
                     CGImageRef cgImg = [repr fullResolutionImage];
                     NSString *fname = repr.filename;
                     UIImage *img = [UIImage imageWithCGImage:cgImg];
                     NSData *data = UIImagePNGRepresentation(img);
                     [data writeToFile:[baseDir stringByAppendingPathComponent:fname]
                            atomically:YES];
                     //FOR LOCAL URL OF THE IMAGE
                     //NSString *imageURL = [baseDir stringByAppendingPathComponent:fname];
                     //UIImage *myImg =[UIImage imageWithContentsOfFile:imageURL];
                     //NSLog(@"%@ URL OF IMAGE ",imageURL);
                     NSLog(@"Image %d has %d size",i,data.length);
                     _sizeOfSelectedImage   +=data.length;
                     NSLog(@"%d is the size",_sizeOfSelectedImage);

                 }
                             failureBlock:^(NSError *error){

                }];
        }

        NSLog(@"COPIED %lu FILE INTO LOCAL MEMORY AND TOTAL SIZE COPIED IS %d ",(unsigned    long)assets.count,_sizeOfSelectedImage);

最佳答案

方法 assetForURL:resultBlock:failureBlock: 将异步执行 Assets 加载。这就是为什么我们首先执行底部的 NSLog,然后在 Block 中执行。如果你想让它同步执行,这样做:

   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) ^{
        ALAssetsLibrary *lib=[ALAssetsLibrary new];  
        _sizeOfSelectedImage=0;  

        dispatch_group_t group = dispatch_group_create();  

        for (int i=0;i<assets.count;i++) {  
                ALAsset *asset=assets[i];  
                FileOP *fileMgr=[[FileOP alloc]init];  
                NSString *baseDir=[fileMgr GetDocumentDirectory];  

                //STORING FILE INTO LOCAL
                dispatch_group_enter(group);  
                [lib assetForURL:asset.defaultRepresentation.url
                     resultBlock:^(ALAsset *asset){
                         ALAssetRepresentation *repr = [asset defaultRepresentation];
                         CGImageRef cgImg = [repr fullResolutionImage];
                         NSString *fname = repr.filename;
                         UIImage *img = [UIImage imageWithCGImage:cgImg];
                         NSData *data = UIImagePNGRepresentation(img);
                         [data writeToFile:[baseDir stringByAppendingPathComponent:fname]
                                atomically:YES];
                         //FOR LOCAL URL OF THE IMAGE
                         //NSString *imageURL = [baseDir stringByAppendingPathComponent:fname];
                         //UIImage *myImg =[UIImage imageWithContentsOfFile:imageURL];
                         //NSLog(@"%@ URL OF IMAGE ",imageURL);
                         NSLog(@"Image %d has %d size",i,data.length);
                         _sizeOfSelectedImage   +=data.length;
                         NSLog(@"%d is the size",_sizeOfSelectedImage);
                        dispatch_group_leave(group);  
                     }
                     failureBlock:^(NSError *error){
                                dispatch_group_leave(group);  
                    }];
            }
            dispatch_group_wait(group, DISPATCH_TIME_FOREVER);  
            NSLog(@"COPIED %lu FILE INTO LOCAL MEMORY AND TOTAL SIZE COPIED IS %d ",(unsigned    long)assets.count,_sizeOfSelectedImage);
            dispatch_async(dispatch_get_main_queue(), ^{
                 // Do your call back on main thread here
            });
    });  

Edit1:来自 Ken 的增强答案

ALAssetsLibrary *lib=[ALAssetsLibrary new];
_sizeOfSelectedImage=0;

dispatch_group_t group = dispatch_group_create();

for (int i=0;i<assets.count;i++) {
    ALAsset *asset=assets[i];
    FileOP *fileMgr=[[FileOP alloc]init];
    NSString *baseDir=[fileMgr GetDocumentDirectory];

    //STORING FILE INTO LOCAL
    dispatch_group_enter(group);
    [lib assetForURL:asset.defaultRepresentation.url
         resultBlock:^(ALAsset *asset){
             ALAssetRepresentation *repr = [asset defaultRepresentation];
             CGImageRef cgImg = [repr fullResolutionImage];
             NSString *fname = repr.filename;
             UIImage *img = [UIImage imageWithCGImage:cgImg];
             NSData *data = UIImagePNGRepresentation(img);
             [data writeToFile:[baseDir stringByAppendingPathComponent:fname]
                    atomically:YES];
             //FOR LOCAL URL OF THE IMAGE
             //NSString *imageURL = [baseDir stringByAppendingPathComponent:fname];
             //UIImage *myImg =[UIImage imageWithContentsOfFile:imageURL];
             //NSLog(@"%@ URL OF IMAGE ",imageURL);
             NSLog(@"Image %d has %d size",i,data.length);
             _sizeOfSelectedImage   +=data.length;
             NSLog(@"%d is the size",_sizeOfSelectedImage);
             dispatch_group_leave(group);
         }
        failureBlock:^(NSError *error){
            dispatch_group_leave(group);
        }];
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    // Do your call back on main thread here
    NSLog(@"COPIED %lu FILE INTO LOCAL MEMORY AND TOTAL SIZE COPIED IS %d ",(unsigned    long)assets.count,_sizeOfSelectedImage);

    // Your code here
});

关于ios - 程序流程出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24857150/

相关文章:

ios - Xcode 9 ios 11隐藏控制中心?

ios - NSFetchedController 和部分

ios - 无法让 UIScrollView 工作

ios - iOS 动画 block 如何工作?

objective-c - 嵌套 objective-c block 时的语法/格式

ios - 当对象在ios中执行方法时,是否有发布通知的解决方案

ios - Swift3 中的 "Cannot convert value of type"错误

ios - UIDocument openWithCompletionHandler 未在 iOS 设备上完成

ios - 如何在运行时确定 iOS 设备是否支持蓝牙 LE

objective-c - 为什么 objective-c 没有 API 可用性检查?