【发布时间】:2014-03-11 04:49:52
【问题描述】:
从相册中导入多张照片,其中一种委托方法是
// 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",idx+1,[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 10
Label value->2 of 10
Label value->3 of 10
Label value->4 of 10
Label value->5 of 10
Label value->6 of 10
Label value->7 of 10
Label value->8 of 10
Label value->9 of 10
Label 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);
}
上面的函数在主线程上工作并更新,但仍然没有显示标签,它在 NSLog 之后打印
start
Updating ui->1 of 10
Finish
start
Updating ui->2 of 10
Finish
start
Updating ui->3 of 10
Finish
start
Updating ui->4 of 10
Finish
start
Updating ui->5 of 10
Finish
start
Updating ui->6 of 10
Finish
start
Updating ui->7 of 10
Finish
start
Updating ui->8 of 10
Finish
start
Updating ui->9 of 10
Finish
start
Updating ui->10 of 10
Finish
我已将项目上传至this location,请随时提供帮助。
【问题讨论】:
-
你确定label连接对了吗,检查nil?
-
可能在更新 UI 之前,pickerview 已被解除并从内存中释放。
-
@DmitryShevchenko 如果我 nslog 标签值它会打印 n 中的 1、n 中的 2 等等
-
@whitewolf09 AFAIK pickerview 与 UI 更新无关,您能说得更具体些吗?
-
@SatishAzad 框架是 {{40,29},{100,21}},是的,这是我找不到的问题,所以向 GURU 寻求帮助:)。
标签: ios iphone objective-c objective-c-blocks