我正在从ImagePicker上传图像,并且一旦图像被上传到服务器,我就会从URL显示它。因此,从本地图像到URL图像都会出现闪烁,我想避免该闪烁,但无法修复。任何建议都会非常有帮助。我在下面添加代码:
- (void)setDataOnCell:(NSDictionary *)dict {
self.messageTimeLabel.text = [CommonUtils checkForNUllValue:[dict valueForKey:@"msg_time"]];
if (![[CommonUtils checkForNUllValue:[dict valueForKey:@"msg_status"]] isEqualToString:@"Read"]) {
self.messageTickImageView.image = [UIImage imageNamed:@"check_delivered_icon"];
self.messageStatusLabel.textColor = [UIColor colorWithRed:166.0f/255.0f green:166.0f/255.0f blue:166.0f/255.0f alpha:1.0];
}
else {
self.messageTickImageView.image = [UIImage imageNamed:@"check_read_icon"];
self.messageStatusLabel.textColor = [UIColor colorWithRed:254.0f/255.0f green:223.0f/255.0f blue:224.0f/255.0f alpha:1.0];
}
self.messageStatusLabel.text = [CommonUtils checkForNUllValue:[dict valueForKey:@"msg_status"]];
if ([[NSString stringWithFormat:@"%@", [dict valueForKey:@"attachment_type"]] isEqualToString:@"local_img"]){
self.messageImageview.image = [dict valueForKey:@"attachment"];
}
else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
^{
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [NSString stringWithFormat:@"%@", [dict valueForKey:@"attachment"]]]];
NSData *urlData = [NSData dataWithContentsOfURL:imageURL];
//This is your completion handler
dispatch_sync(dispatch_get_main_queue(), ^{
UIImage *image = [UIImage imageWithData:urlData];
self.messageImageview.image = image;
});
});
}
}
首先,您需要在该图像视图上设置默认图像,然后通过url调用图像并在此图像上进行设置
NSOperationQueue *queueThumbnl = [[NSOperationQueue alloc] init];
[queueThumbnl setMaxConcurrentOperationCount:NSOperationQueueDefaultMaxConcurrentOperationCount];
[queueThumbnl addOperationWithBlock:^{
// Background work
NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@", [NSString stringWithFormat:@"%@", [dict valueForKey:@"attachment"]]]];
NSData *urlData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:urlData];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:self.messageImageview, @"messageImageview", urlData, @"imageData", nil];
[self performSelectorOnMainThread:@selector(mainThreadUIUpdate:) withObject:dict waitUntilDone:YES];
// [self updateThumbnailOnMainThread:anItem setImageTo:cell];
}];
-(void)mainThreadUIUpdate:(NSDictionary *)dict
{
UIImageView *messge = [dict objectForKey:@"messageImageview"];
UIImage *image = [dict objectForKey:@"image"];
[messge.imageView setImage:image];
[cell.imageView setNeedsDisplay:YES];
}