美文网首页
SDWebImage cell无法加载图片,只有滑动tablev

SDWebImage cell无法加载图片,只有滑动tablev

作者: 写代码写到人生巅峰 | 来源:发表于2017-05-22 16:55 被阅读0次

    1、使用了系统的cell会产生这个问题,滑动加载了是去沙盒获取图片

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"cell"];
    }
    [cell.imageView sd_setImageWithURL:_imageURL[indexPath.row]
                      placeholderImage:nil
                               options:SDWebImageRetryFailed];
    
    return cell;
    

    使用自定义的cell就不会产生这个问题

    TestTableViewCell *testCell = [TestTableViewCell cellWithTableView:tableView];
    [testCell.testImageView sd_setImageWithURL:_imageURL[indexPath.row]
                              placeholderImage:nil
                                       options:SDWebImageRetryFailed];
    
    return testCell;
    

    2、对下载之后的图片要做自己的处理

    SDWebImageDownloaderOperation.m
    
    - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
        @synchronized(self) {
            self.dataTask = nil;
            dispatch_async(dispatch_get_main_queue(), ^{
                [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadStopNotification object:self];
                if (!error) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:SDWebImageDownloadFinishNotification object:self];
                }
            });
        }
        
        if (error) {
            [self callCompletionBlocksWithError:error];
        } else {
            if ([self callbacksForKey:kCompletedCallbackKey].count > 0) {
                /**
                 *  See #1608 and #1623 - apparently, there is a race condition on `NSURLCache` that causes a crash
                 *  Limited the calls to `cachedResponseForRequest:` only for cases where we should ignore the cached response
                 *    and images for which responseFromCached is YES (only the ones that cannot be cached).
                 *  Note: responseFromCached is set to NO inside `willCacheResponse:`. This method doesn't get called for large images or images behind authentication
                 */
                if (self.options & SDWebImageDownloaderIgnoreCachedResponse && responseFromCached && [[NSURLCache sharedURLCache] cachedResponseForRequest:self.request]) {
                    // hack
                    [self callCompletionBlocksWithImage:nil imageData:nil error:nil finished:YES];
                } else if (self.imageData) {
                    
                    /******************** 在这里对图片进行处理 ********************/
                    //  比如我的是在 下载之后的图片数据 进行blowfish解密
                    NSDictionary *imageDataDict    = [NSJSONSerialization JSONObjectWithData:self.imageData options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
                    NSString *base64String         = imageDataDict[@"data"];
                    NSData *decodedData            = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
                    NSData *keyData                = [JAX_kBlowFishKey dataUsingEncoding:NSUTF8StringEncoding];
                    NSData *imageData              = [NSData blowfishDecrypt:decodedData usingKey:keyData];
                    self.imageData = imageData.mutableCopy;
                    
                  /******************** 在这里对图片进行处理 ********************/
                    UIImage *image = [UIImage sd_imageWithData:self.imageData];
                    NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL];
                    image = [self scaledImageForKey:key image:image];
                    
                    // Do not force decoding animated GIFs
                    if (!image.images) {
                        if (self.shouldDecompressImages) {
                            if (self.options & SDWebImageDownloaderScaleDownLargeImages) {
    #if SD_UIKIT || SD_WATCH
                                image = [UIImage decodedAndScaledDownImageWithImage:image];
                                [self.imageData setData:UIImagePNGRepresentation(image)];
    #endif
                            } else {
                                image = [UIImage decodedImageWithImage:image];
                            }
                        }
                    }
                    if (CGSizeEqualToSize(image.size, CGSizeZero)) {
                        [self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]];
                    } else {
                        [self callCompletionBlocksWithImage:image imageData:self.imageData error:nil finished:YES];
                    }
                } else {
                    [self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Image data is nil"}]];
                }
            }
        }
        [self done];
    }
    

    相关文章

      网友评论

          本文标题:SDWebImage cell无法加载图片,只有滑动tablev

          本文链接:https://www.haomeiwen.com/subject/soqixxtx.html