我想創建一個UIPickerView,其中包含一些圖像,但我似乎無法弄清楚如何讓圖像適合視圖(現在它們太大了,相互重疊)。我試圖使用函數來調整每個圖像時,它的繪製,但我收到錯誤時,函數被調用,雖然程序編譯和運行良好(圖像不調整大小除外)。調整大小功能和初始化功能是:在UIImageView中調整UIImage的大小
-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height {
NSLog(@"resizing");
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
//if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef),
4 * width, CGImageGetColorSpace(imageRef), alphaInfo);
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
- (void)viewDidLoad {
UIImage *h1 = [UIImage imageNamed:@"h1.png"];
h1 = [self resizeImage:h1 width:50 height: 50];
UIImageView *h1View = [[UIImageView alloc] initWithImage:h1];
NSArray *imageViewArray = [[NSArray alloc] initWithObjects:
h1View, nil];
NSString *fieldName = [[NSString alloc] initWithFormat:@"column1"];
[self setValue:imageViewArray forKey:fieldName];
[fieldName release];
[imageViewArray release];
[h1View release];
}
控制檯輸出:
TabTemplate [29322:207]調整
TabTemplate [29322]:CGBitmapContextCreate:不支持的色彩空間
TabTemplate [ 29322]:CGContextDrawImage:invalid context
TabTempla te [29322]:CGBitmapContextCreateImage:無效的上下文
我找不出什麼問題。任何幫助是極大的讚賞。
情況下,你是否通過CGImageGetColorSpace(imageRef)返回的值是CGBitmapContextCreate接受的價值觀嗎? – Bemmu 2011-06-17 05:20:05