ios – 从块更新uilabel

ios – 从块更新uilabel,第1张

概述从相册导入多张照片,其中一种委托方法是 // Here info is array of dictionary containing FileName/AssetURL etc- (void)somePicker(SomePicker*)somePicker didFinishPickingMediaWithInfo:(NSArray *)info { _importStatusView 从相册导入多张照片,其中一种委托方法是

// Here info is array of dictionary containing filename/AssetURL etc- (voID)somePicker(SomePicker*)somePicker dIDFinishPickingMediawithInfo:(NSArray *)info {    _importStatusVIEw.center = self.vIEw.center;    [self.vIEw addSubvIEw:_importStatusVIEw];    [self dismissVIEwControllerAnimated:YES completion:^{        NSNumber *total = [NSNumber numberWithInteger:info.count];        [info enumerateObjectsUsingBlock:^(ID obj,NSUInteger IDx,BOol *stop) {            NSDictionary *imageInfo = (NSDictionary*)obj;            Nsstring *filename = [imageInfo objectForKey:@"UIImagePickerControllerfilename"];            NSURL *imageURL = [imageInfo objectForKey:UIImagePickerControllerReferenceURL];            ALAssetsLibrary *assetlibrary=[[ALAssetsLibrary alloc] init];            [assetlibrary assetForURL:imageURL resultBlock:^(ALAsset *asset) {                NSLog(@"start");                ALAssetRepresentation *rep = [asset defaultRepresentation];                Byte *buffer = (Byte*)malloc(rep.size);                NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];                NSData *data = [NSData dataWithBytesNocopy:buffer length:buffered freeWhenDone:YES];                Nsstring *filePath = [_currentPath stringByAppendingPathComponent:filename];                [data writetofile:filePath atomically:YES];                //This also has no effect                 //dispatch_async(dispatch_get_main_queue(),^{                    //_lblimportCountStatus.text = [Nsstring stringWithFormat:@"%d of %d",IDx+1,[total integerValue]];                    //NSLog(@"Label value->%@",_lblimportCountStatus.text); //This prints values but after everything is finished it prints all line at once i.e. at the end of the enumeration of all items                 //});            //Update UI            NSNumber *current = [NSNumber numberWithInteger:IDx+1];            NSDictionary *status = [NSDictionary dictionaryWithObjectsAndKeys:current,@"current",total,@"totalCount",nil];            [self performSelectorOnMainThread:@selector(updateimportCount:) withObject:status waitUntilDone:YES];                //_lblimportCountStatus.text = [Nsstring stringWithFormat:@"%d of %d",[total integerValue]];                if(IDx==info.count-1){                    [_importStatusVIEw removeFromSupervIEw];                }                NSLog(@"Finish");            } failureBlock:^(NSError *error) {                NSLog(@"Error: %@",[error localizedDescription]);            }];        }];    }];}

我的状态视图和标签声明是

@property (strong,nonatomic) IBOutlet UIVIEw *importStatusVIEw; //VIEw containing label @property (weak,nonatomic) IBOutlet UILabel *lblimportCountStatus; //Label

上面的代码中的所有内容都正常工作并且正如预期的那样,但问题是importStatusVIEw被添加到屏幕但是lblimportCountStatus值没有显示,但是如果我记录它显示更新的值.

当枚举在最后完成时,所有NSLog都被打印出来,例如,如果我导入了10张照片而不是最后打印的照片,即dispatch_async(dispatch_get_main_queue()这个函数在枚举正在进行时完全没有效果.

Label value->1 of 10Label value->2 of 10Label value->3 of 10Label value->4 of 10Label value->5 of 10Label value->6 of 10Label value->7 of 10Label value->8 of 10Label value->9 of 10Label value->10 of 10

可能是什么问题 ?

更新:

-(voID)updateimportCount:(NSDictionary*)info{ //(NSNumber*)current forTotalitems:(NSNumber*)totalCount{    NSNumber *current = [info objectForKey:@"current"];    NSNumber *totalCount = [info objectForKey:@"totalCount"];    _lblimportCountStatus.text = [Nsstring stringWithFormat:@"%d of %d",[current integerValue],[totalCount integerValue]];    [_lblimportCountStatus setNeedsdisplay];    NSLog(@"Updating ui->%@",_lblimportCountStatus.text);}

上面的函数适用于主线程和更新,但stil标签未显示,它在NSLog之后打印

startUpdating ui->1 of 10FinishstartUpdating ui->2 of 10FinishstartUpdating ui->3 of 10FinishstartUpdating ui->4 of 10FinishstartUpdating ui->5 of 10FinishstartUpdating ui->6 of 10FinishstartUpdating ui->7 of 10FinishstartUpdating ui->8 of 10FinishstartUpdating ui->9 of 10FinishstartUpdating ui->10 of 10Finish

我已经在this location上传了项目,请随时提供帮助.

解决方法 所有这些块都在主线程上执行(最简单的方法是验证这是使用NSThread的currentThread来获取当前线程,而-isMainThread来检查它是否是主线程.你有代码的任何地方你想看看它在哪个线程上,做这样的事情:

NSLog( @"enumeration block on main thread: %@",[[NSThread currentThread] isMainThread] ? @"YES" : @"NO" );

我认为问题是,因为这都是在主线程上执行的,所以你锁定了runloop,而没有让UI有机会更新.

修复此问题的正确方法可能是在单独的线程上进行此处理(通过performSelectorOnMainThread:调用代码来更新UI,正如您现在所做的那样).但是,让它工作的快速入侵就是允许runloop运行.在updateimportCount:结束时,执行以下 *** 作:

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

它不漂亮,但它会起作用.

更新:
在Cocoa(Mac OS和iOS)中有一个runloop的概念.应用程序的主线程驱动NSRunLoop,它充当事件循环.用户 *** 作 – 分接头等 – 通过此循环进行处理,如定时器,网络连接和其他内容.有关详细信息,请参阅NSRunLoop Reference.

在iOS中,绘图也发生在runloop上.因此,当您在视图上调用setNeedsdisplay时,不会立即重绘该视图.相反,它只是标记为需要重绘,然后在下一个绘图周期(下一次通过runloop的行程)中进行实际绘图. UIView reference对此进行了简要描述(参见“查看绘图周期”部分.引用该部分:

When the actual content of your vIEw changes,it is your
responsibility to notify the system that your vIEw needs to be
redrawn. You do this by calling your vIEw’s setNeedsdisplay or
setNeedsdisplayInRect: method of the vIEw. These methods let the
system kNow that it should update the vIEw during the next drawing
cycle. Because it waits until the next drawing cycle to update the
vIEw,you can call these methods on multiple vIEws to update them at
the same time.

当您从响应某些用户 *** 作调用的任何方法返回时(在这种情况下,somePicker:dIDFinishPickingMediawithInfo :,控件返回到runloop,并且任何需要重绘的视图都是.但是,如果您对此进行了大量处理主线程没有返回,runloop基本上停止了,并且不会进行绘图.上面的代码基本上给了runloop一些时间来处理,所以可以进行绘制.[NSDate date]返回当前的日期和时间,现在,所以这基本上告诉runlooop“直到现在运行”,最终给它一个循环通过循环,给你一个绘图周期,这是一个重绘你的标签的机会.

总结

以上是内存溢出为你收集整理的ios – 从块更新uilabel全部内容,希望文章能够帮你解决ios – 从块更新uilabel所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069545.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存