2011-09-11 96 views
0

嗨,所以我目前有一個小圖像(約100x160)作爲我的CoreData模型中的NSData屬性。顯示一個圖像,必須在UITableViewCell中調整大小

我在一個TableView中顯示所有實體。單個Cell中的UIImageView只有50x80的大小。只是將圖像放入此框架看起來有點像卵石。

什麼是在我的tableViewCell中顯示此圖像的最佳解決方案?在我的cellForRowAtIndexPath中即時調整它的大小?可能這會導致我的桌面視圖變得有點遲鈍。

在創建時調整它的大小,並將其保存在我的coredata實體(或可能在磁盤上)?

謝謝!如果有不明之處請發表評論

回答

0

爲此,您必須裁剪/調整圖像大小。以下是根據所需幀裁剪圖像的代碼。

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

你可以用任何方式去。

+0

我的問題不是如何調整它,而是應該在滾動時調整它的大小。 – choise

+0

是的,你應該。一旦緩存了調整大小的圖像,您可以從緩存中引用圖像,而不是每次調整圖像大小。 –

+0

奇怪的是。通過我的電視快速滾動顯示此列中的錯誤圖像。有時他們是正確的(在第一次加載時)正常滾動感覺有時是正確的,快速滾動在某些單元格中顯示錯誤的圖像。任何想法爲什麼會發生? – choise

相關問題