Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UITableCellView中从web加载图片需要很长时间_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 在UITableCellView中从web加载图片需要很长时间

Ios 在UITableCellView中从web加载图片需要很长时间,ios,objective-c,uitableview,Ios,Objective C,Uitableview,在我的应用程序中,我正在读取一个文件,其中存储了一些图片的URL。我将展示以下图片: 为此,我编写了以下代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"HistoryCell"; HistoryCustomCell *historyCusto

在我的应用程序中,我正在读取一个文件,其中存储了一些图片的URL。我将展示以下图片:

为此,我编写了以下代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"HistoryCell";
    HistoryCustomCell *historyCustomCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    historyCustomCell.labelTitle.text = [sortedArray[indexPath.row] objectForKey:@"title"];
    historyCustomCell.labelContent.text = [sortedArray[indexPath.row] objectForKey:@"text"];
    if ([[sortedArray[indexPath.row] objectForKey:@"date"]isEqualToString:stringDate]) {
        historyCustomCell.labelDate.text = [[sortedArray[indexPath.row] objectForKey:@"time"] stringByReplacingOccurrencesOfString:@" " withString:@""];
    } else {
        historyCustomCell.labelDate.text = [sortedArray[indexPath.row] objectForKey:@"date"];
    }

    if (![[sortedArray[indexPath.row] objectForKey:@"imgUrl"] isEqualToString:@""]) {
        CALayer *cellImageLayer = historyCustomCell.imageView.layer;
        [cellImageLayer setCornerRadius:33.5];
        [cellImageLayer setMasksToBounds:YES];

        if ([[NSString stringWithFormat:@"%@",[sortedArray[indexPath.row]objectForKey:@"suggested"] ] isEqualToString:@"1"] && [[NSString stringWithFormat:@"%@",[sortedArray[indexPath.row]objectForKey:@"breakingNews"] ] isEqualToString:@"0"]) {
            // SUGGESTED
            [cellImageLayer setBorderColor:[[UIColor colorWithRed:161.0 green:199.0 blue:0 alpha:1.0] CGColor]];
            [cellImageLayer setBorderWidth:3.0];
        }
        if ([[NSString stringWithFormat:@"%@",[sortedArray[indexPath.row]objectForKey:@"breakingNews"] ] isEqualToString:@"1"] && [[NSString stringWithFormat:@"%@",[sortedArray[indexPath.row]objectForKey:@"suggested"] ] isEqualToString:@"0"]) {
            // BREAKING NEWS
            [cellImageLayer setBorderColor:[[UIColor colorWithRed:254.0 green:214.0 blue:0 alpha:1.0] CGColor]];
            [cellImageLayer setBorderWidth:3.0];
        }
        if ([[sortedArray[indexPath.row] objectForKey:@"is_unread"] intValue] == 1) {
            historyCustomCell.contentView.superview.backgroundColor = [UIColor whiteColor];
        } else {
            historyCustomCell.contentView.superview.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0];
        }

        NSURL *imgUrl = [NSURL URLWithString:[sortedArray[indexPath.row] objectForKey:@"imgUrl"]];
        UIImage *imgThumb = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
        CGSize itemSize = CGSizeMake(67, 67);
        UIGraphicsBeginImageContext(itemSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [imgThumb drawInRect:imageRect];
        historyCustomCell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    } else {
        [historyCustomCell.imageView setImage:nil];
    }

    return historyCustomCell;
}
什么时候 我尝试在设备上运行应用程序,视图将像图像一样绘制,但加载图片需要太多时间,UITableView滚动速度非常慢。我想这是一个问题,因为接收图像的连接,我如何修复它

更新 我想我可以用这个方法来解决我的问题:

- (void) loadImageFromWeb:(NSURL *)urlImg andImageView:(UIImageView *)imageView {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:urlImg];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response,
                                               NSData * data,
                                               NSError * error) {
                               if (!error){
                                   UIImage *imgThumb = [UIImage imageWithData:data];
                                   CGSize itemSize = CGSizeMake(50, 50);
                                   UIGraphicsBeginImageContext(itemSize);
                                   CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
                                   [imgThumb drawInRect:imageRect];
                                   imageView.image = UIGraphicsGetImageFromCurrentImageContext();
                                   UIGraphicsEndImageContext();
                                   [hud hide:YES];
                               } else {
                                   NSLog(@"ERRORE: %@", error);
                                   [hud hide:YES];
                               }

                           }];
}
使用此方法,它应该在GUI上无延迟地加载UIImageView,对吗

UIImage *imgThumb = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
上面的线路是一个同步呼叫。这意味着,在此行完成从服务器下载所有数据并将其转换为UIImage之前,其余代码将不会运行。您要做的是一个异步请求

您可以搜索如何执行异步URL请求。通常,这涉及一个委托模式或完成块,一旦图像下载完成,它就会执行。这将允许其余代码运行,并且应用程序的UI不会冻结。

问题出在这里

UIImage*imgThumb=[UIImage-imageWithData:[NSData-dataWithContentsOfURL:imgUrl]]

从文件中

dataWithContentsOfURL

此方法非常适合将data://URL转换为NSData对象, 也可用于同步读取短文件。不要使用 此同步方法用于请求基于网络的URL。对于 基于网络的URL,此方法可以阻止当前线程数十次 在慢速网络上的秒数,导致较差的用户体验,以及 在iOS中,可能导致您的应用程序终止

要下载图像并保持平滑滚动行为,您需要异步下载图像。在您的情况下,发生的是在下载映像之前,主线程上的执行被阻止。为了避免这种情况,您必须在单独的线程上下载图像,下载完成后,获取UIImage对象并将其设置为相应的单元格

有很多方法可以下载。非常古老的方法是使用NSOperations,或者按照文档的建议,使用而不是dataWithContentsOfURL

如果您使用的是AFNetworking,它有一个Wite UIImageView类别。把它包括在内,然后打电话

[historyCustomCell.imageView setImageWithURL:imgUrl]

这将异步下载并缓存图像。

您正在使用同步过程下载图像尝试使用异步图像下载尝试查看我的编辑,我想这可能是一个很好的解决方案。我解决了它,谢谢!欢迎光临,祝您愉快