문제

I need to stretch a large pic to show it as a background in an iPad app. Since I download it from the net, I'm using SDWebImage. In the container view controller I instantiate two UIImageViews, one (bgimgvu) that will hold the resized bg, and one (loadvu, invisible) to load the pic. I'm using the following code:

    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 );
    }];

After this, I instantiate another view controller to hold other graphical UI elements, among which there is another pic downloaded from the internet. Then I add it to self.view.

The problem is: this callback function

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

gets called, but with the wrong image as argument! That is, it gets called with the second pic as argument.

For completeness' sake, the function which resizes the downloaded image is:

- (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;
}

What am I missing/doing wrong?

도움이 되었습니까?

해결책

I worked it out by adding a comparison at the beginning of the success callback function:

if(image!=loadvu.image) return; 

This gives the following warning:

Capturing 'loadvu' strongly in this block is likely to lead to a retain cycle

But compiles anyway. Not a clean solution, just a working solution.

다른 팁

The same issue occurs on a failure when there are multiple blocks for SDWebImage being called at the same time. I'm pretty sure this is a bug with SDWebImage.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top