我需要拉伸一個大的圖片,以在iPad應用程序中將其顯示爲背景。由於我從網上下載它,我使用SDWebImage。 在容器視圖控制器中,我實例化兩個UIImageViews,其中一個(bgimgvu
)將容納調整後的bg,另一個(loadvu
,不可見)加載圖片。我使用下面的代碼:SDWebImage成功加載處理程序返回一個錯誤的UIImage
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);
}];
在此之後,我實例化另一個視圖控制器持有其他圖形用戶界面元素,其中有從互聯網上下載的其他照片。然後我將它添加到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;
}
我缺少/做錯了嗎?