程序流程出错 [英] The program flow going wrong

查看:55
本文介绍了程序流程出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In this code snippet the flow of program goes out of for loop first then only goes inside the block
resultBlock:^(ALAsset *asset). The code prints the NSLog at the bottom first then executes the block inside the loop. What's happening here?

 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: 将异步执行资产的加载.这就是为什么我们首先执行底部 NSLog,然后在 Block 中执行.如果你想让它同步执行,就这样做:

Method assetForURL:resultBlock:failureBlock: will execute the load of asset asynchronously. That's why we first have the bottom NSLog executed and then in Block. If you want it to be executed synchronously, do it like this:

   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 的增强答案

Enhanced answer from 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
});

这篇关于程序流程出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆