如何使用 nscache 从 API 下载镜像



我在UICollectionview单元格中有一个UIImageview,并从api解析图像数据并添加到UIImageview。现在一切都很完美,但是当我尝试滚动单元格时,我在向下滚动时遇到了一些问题UICollectionview它挂起了。

现在我需要使用 NSCache 下载图像并将其添加到 UIImage ,但我无法完成此任务,我被困在我的代码中的某个地方,我不知道在哪里。请查看我的代码。

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
OneTimeCell *cell = (OneTimeCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.selected = YES;
 [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
cell.productname.text = [[productname objectAtIndex:indexPath.row] objectForKey:@"product_name"];
cell.productnumber.text = [[productname objectAtIndex:indexPath.row] objectForKey:@"id"];

NSString *image = [[productname objectAtIndex:indexPath.row] objectForKey:@"image" ];
NSLog(@"image  %@", image);
NSURL *baseURL = [NSURL URLWithString:@"http://dev1.brainpulse.org/ecoware1//app/webroot/img/uploads/Product/thumb/"];
NSURL *url = [NSURL URLWithString:image relativeToURL:baseURL];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * productimage = [UIImage imageWithData:imageData];
NSURL *absURL = [url absoluteURL];
NSLog(@"absURL = %@", absURL);

if (productimage) {
       cell.productimage.image = productimage;
}
else
{
    cell.productimage.image = [UIImage imageNamed:@"icon_1.png"];

    [self.imageDownloadingQueue addOperationWithBlock:^{
        NSURL *url = [NSURL URLWithString:image relativeToURL:baseURL];
        NSData * imageData = [NSData dataWithContentsOfURL:url];
        UIImageView *image = nil;
        if (image) {
            [self.imageCache setObject:image forKey:imageData];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                OneTimeCell *cell =[collectionView cellForItemAtIndexPath:indexPath];
                if (cell) {
                    cell.productimage.image = image;
                }
            }];
        }

    }];
      }


     return cell;
  }
在您的

代码行 10 中,您在主线程上获取图像,这就是您的单元格卡住的原因......请用这个代码替换该代码

替换此代码

NSString *image = [[productname objectAtIndex:indexPath.row] objectForKey:@"image" ];
NSLog(@"image  %@", image);
NSURL *baseURL = [NSURL URLWithString:@"http://dev1.brainpulse.org/ecoware1//app/webroot/img/uploads/Product/thumb/"];
NSURL *url = [NSURL URLWithString:image relativeToURL:baseURL];
NSData * imageData = [NSData dataWithContentsOfURL:url];
UIImage * productimage = [UIImage imageWithData:imageData];
NSURL *absURL = [url absoluteURL];
NSLog(@"absURL = %@", absURL);

有了这个

 NSString *image = [[productname objectAtIndex:indexPath.row] objectForKey:@"image" ];
NSLog(@"image  %@", image);
NSURL *baseURL = [NSURL URLWithString:@"http://dev1.brainpulse.org/ecoware1//app/webroot/img/uploads/Product/thumb/"];
NSURL *url = [NSURL URLWithString:image relativeToURL:baseURL];
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];
    dispatch_async(dispatch_get_main_queue(), ^{
UIImage * productimage = [UIImage imageWithData:imageData];
    });
}); 
NSURL *url = @"................";
ImageRequest *request = [[ImageRequest alloc] initWithURL:url];
UIImage *image = [request cachedResult];
if (image) {
     // Set the image if exist
} else {
    [request startWithCompletion:^(UIImage *image, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{
  if(image) {
    cell.productimage.image = productimage;
   }else {
   // Set any placeholder image here.......
}
});
}];
}
NSString *image = [[productname objectAtIndex:indexPath.row] objectForKey:@"image" ];
NSLog(@"image  %@", image);
NSURL *baseURL = [NSURL URLWithString:@"http://dev1.brainpulse.org/ecoware1//app/webroot/img/uploads/Product/thumb/"];
NSURL *url = [NSURL URLWithString:image relativeToURL:baseURL];
dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{
    //NSData * imageData = [NSData dataWithContentsOfURL:url];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:url] queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if(data)
         {
             dispatch_async(dispatch_get_main_queue(), ^{
                 UIImage * productimage = [UIImage imageWithData:data];
             });
         }
     }];
});

最新更新