爲此,您必須裁剪/調整圖像大小。以下是根據所需幀裁剪圖像的代碼。
- (void)viewDidLoad
{
[super viewDidLoad];
// do something......
UIImage *img = [UIImage imageWithData:(nsdata)]; // nsdata will be your image data as you specified.
// To crop Image
UIImage *croppedImage = [self imageByCropping:img] toRect:CGRectMake(10, 10, 50, 80)];
// To resize image
UIImage *resizedImage = [self resizeImage:img width:50 height:80];
}
裁切圖像:
- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
return cropped;
}
調整圖像大小:
-(UIImage *)resizeImage:(UIImage *)image width:(int)width height:(int)height
{
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
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];
return result;
}
你可以用任何方式去。
我的問題不是如何調整它,而是應該在滾動時調整它的大小。 – choise
是的,你應該。一旦緩存了調整大小的圖像,您可以從緩存中引用圖像,而不是每次調整圖像大小。 –
奇怪的是。通過我的電視快速滾動顯示此列中的錯誤圖像。有時他們是正確的(在第一次加載時)正常滾動感覺有時是正確的,快速滾動在某些單元格中顯示錯誤的圖像。任何想法爲什麼會發生? – choise