1

我需要拉伸一张大图片以将其显示为 iPad 应用程序中的背景。由于我是从网上下载的,所以我使用的是 SDWebImage。在容器视图控制器中,我实例化了两个 UIImageView,一个 ( bgimgvu) 将保存调整大小的 bg,一个 ( loadvu,不可见) 用于加载图片。我正在使用以下代码:

    bgimgvu = [[UIImageView alloc] init];
    bgimgvu.contentMode = UIViewContentModeBottomRight;
    bgimgvu.frame = CGRectMake(0, 44, 1024, 675);
    [self.view addSubview:bgimgvu]; 

    UIImageView * loadvu = [[UIImageView alloc] init];
    [loadvu setImageWithURL:[NSURL URLWithString:urls] placeholderImage:[UIImage imageNamed:@"placeholder_small.png"] success:^(UIImage *image) {
            UIImage * bgi = [self resizeImage:image newSize:CGSizeMake(1500, 675)];
            if (bgi!=nil) {
                [bgimgvu setImage:bgi];
            }
        } failure:^(NSError *error2) {
            NSLog(@"*** Background loading error: %@", error2 );
    }];

之后,我实例化了另一个视图控制器来保存其他图形 UI 元素,其中还有一张从 Internet 下载的图片。然后我将它添加到self.view.

问题是:这个回调函数

        success:^(UIImage *image) {
            UIImage * bgi = [self resizeImage:image newSize:CGSizeMake(1500, 675)];
            if (bgi!=nil) {
                [bgimgvu setImage:bgi];
            }

被调用,但使用错误的图像作为参数!也就是说,它以第二张图片作为参数调用。

为了完整起见,调整下载图像大小的函数是:

- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGImageRef imageRef = image.CGImage;

    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);

    CGContextConcatCTM(context, flipVertical);  
    // Draw into the context; this scales the image
    CGContextDrawImage(context, newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    CGImageRelease(newImageRef);
    UIGraphicsEndImageContext();    

    return newImage;
}

我错过了什么/做错了什么?

4

2 回答 2

0

我通过在成功回调函数的开头添加一个比较来解决这个问题:

if(image!=loadvu.image) return; 

这给出了以下警告:

在此块中强烈捕获“loadvu”可能会导致保留周期

但无论如何编译。不是一个干净的解决方案,只是一个有效的解决方案。

于 2012-04-04T05:56:41.623 回答
0

当同时调用 SDWebImage 的多个块时,失败时会出现相同的问题。我很确定这是 SDWebImage 的一个错误。

于 2012-04-18T00:33:33.763 回答