hOW 从 URL 设置图像而不会有任何闪烁

问题描述 投票:0回答:1

我正在从 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;
            });
        });
        
        
    }
}
ios objective-c cocoa-touch uiimageview
1个回答
0
投票

首先,您需要在此图像视图上设置默认图像,然后通过 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];
}
© www.soinside.com 2019 - 2024. All rights reserved.