从网址快速加载图片

时间:2013-08-23 11:38:16

标签: objective-c image

我已经使用SDWebimages类将图像从url加载到图像视图。但它仍然需要时间来加载。

 NSString *filePath1 = [NSString stringWithFormat:@"%@",pathimage1];
 [cell.image1 setImageWithURL:[NSURL URLWithString:filePath1]placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

谁能告诉我我做错了什么?或者从网址加载图片的最佳方式。

2 个答案:

答案 0 :(得分:0)

试试这个:

NSString *filePath1 = [NSString stringWithFormat:@"%@",pathimage1];

[cell.image1 setImageWithURL:[NSURL URLWithString:filePath1]]
                placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                         success:^(UIImage *image, BOOL cached) 
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                cell.image1.image = image;
            });
            NSLog(@"success Block");
        }
                         failure:^(NSError *error) 
        {
            NSLog(@"failure Block");
        }];

答案 1 :(得分:0)

从网络加载图片可能需要很长时间才能加载,原因有两个:最明显的两个原因是

  • 设备上的互联网连接速度慢(Wifi / 3G / 2G)。
  • 服务器过载。

如果您可以控制服务器及其上存储的图像,您可以随时加载较小的版本(低质量的预览/缩略图),然后在需要时加载较大/全尺寸的版本,以加快速度。 :

typedef enum {
  MyImageSizeSmall,
  MyImageSizeMedium,
  MyImageSizeLarge
} MyImageSize;

-(void)requestAndLoadImageWithSize:(MyImageSize)imageSize intoImageView:(UIImageView *)imageView
{
  NSString *imagePath
  switch (imageSize)
  {
    case MyImageSizeSmall:
      imagePath = @"http://path.to/small_image.png";
    break;
    case MyImageSizeMedium:
      imagePath = @"http://path.to/medium_image.png";
    break;
    case MyImageSizeLarge:
      imagePath = @"http://path.to/large_image.png";
    break;
  }
  [imageView setImageWithURL:[NSURL URLWithString:imagePath]
            placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
}