2009-06-02 217 views
7

我有一個標準的UITableViewCell,我使用文本和圖像屬性來顯示favicon.ico和一個標籤。由於UIImage支持ICO格式,所以在大部分情況下,這種方法非常有效。但是,有些網站(如Amazon.com說)具有favicon.ico,它們利用ICO格式在同一文件中存儲多個尺寸的能力。亞馬遜存儲四種不同的尺寸,一直到48x48。在UITableViewCell上設置UIImage尺寸圖像

這導致大多數圖像爲16x16,除了少數以32x32或48x48進來的圖像,並且使所有內容看起來都很糟糕。我在這裏搜索,官方論壇,文檔和其他地方沒有成功。我已經嘗試了所有我能想到的限制圖像大小的方法。唯一有效的方法是無證的方法,我不打算使用。這是我的第一個應用程序,也是我第一次使用Cocoa(來自C#)。

萬一我不是在什麼我要找的明確,理想情況下,建議將圍繞着設置UIImage的尺寸,以便48x48的版本將向下擴展到16×16或方法來告訴UIImage使用ICO文件中存在16x16版本。我不一定需要代碼:只是一個方法的建議會讓我很好。

有沒有人有任何建議? (我在官方論壇as well上問過,因爲我已經沉浸了一天以上,如果有解決方案發布在那裏,我也會把它放在這裏。)

回答

23

下面是用戶在官方論壇posted是最終解決問題(與自己有輕微的修改,使其靜態納入到一套實用方法)「BCD」過:

+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size 
{ 
    UIGraphicsBeginImageContext(size); 
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 
    UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return scaledImage; 
} 
0

我不知道任何關於如何ico文件工作,所以也許可以輕鬆地提取16x16圖像並使用它,但我不知道如何。

據我所知,無法在通用的UITableViewCell的圖像上設置任何有用的屬性。最簡單的方法(特別是如果你是新手)將所有東西縮小到16x16,將是子類UITableViewCell,給它一個UIImageView,總是將圖像視圖的大小設置爲16x16,並將其contentMode設置爲UIViewContentModeAspectFit

+0

我試過這種方法,但沒有奏效,但我可能只是做錯了。 – bbrown 2009-06-02 21:19:06

4

我在我的應用程序中做類似的事情。

UIImage* resizedImage(UIImage *inImage, CGRect thumbRect) 
{ 
    CGImageRef   imageRef = [inImage CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 

    if (alphaInfo == kCGImageAlphaNone) 
     alphaInfo = kCGImageAlphaNoneSkipLast; 

    CGContextRef bitmap = CGBitmapContextCreate (NULL, thumbRect.size.width, thumbRect.size.height, 8, thumbRect.size.width*3, 
                       CGColorSpaceCreateDeviceRGB(), alphaInfo); 

    CGContextDrawImage(bitmap, thumbRect, imageRef); 

    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage* result = [UIImage imageWithCGImage:ref]; 

    CGContextRelease(bitmap); 
    CGImageRelease(ref); 

    return result; 
} 

然後創建一個約束比例的新圖像。用從亞馬遜獲得的ICO文件替換「圖片」。

CGRect sz = CGRectMake(0.0f, 0.0f, 16, 16); 
UIImage *resized = resizedImage(image, sz); 
+0

出於某種原因,您的代碼爲我拋出了很多CoreGraphics錯誤。這可能是ICO格式的問題。雖然這是一個很好的答案。 – bbrown 2009-06-02 21:16:46

+1

他們編譯/鏈接錯誤?我不認爲Xcode默認鏈接到CoreGraphics框架的新項目,所以你需要添加它和相應的@import語句。 – 2009-06-02 21:23:54

+1

是的,你需要鏈接到CoreGraphics框架。如果你不這樣做,那就是你得到這些錯誤的原因。 – 2009-06-02 23:45:57

1

還沒有足夠的業力評論答案,但我上面bbrandons代碼取得了很好的成功。

雖然關於每行字節數設置不夠高 - 我在閱讀這個post的答案後最終將其設置爲0,這讓系統確定了正確的值,但我在控制檯上得到了相當數量的警告。

如:

CGContextRef bitmap = CGBitmapContextCreate(NULL, size.width, size.height, 8, 0, CGColorSpaceCreateDeviceRGB(), alphaInfo); 
1

這裏是我做到了。該技術採用適當移動文本和細節文本標籤的左邊的護理:

@interface SizableImageCell : UITableViewCell {} 
@end 
@implementation SizableImageCell 
- (void)layoutSubviews { 
    [super layoutSubviews]; 

    float desiredWidth = 80; 
    float w=self.imageView.frame.size.width; 
    if (w>desiredWidth) { 
     float widthSub = w - desiredWidth; 
     self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,self.imageView.frame.origin.y,desiredWidth,self.imageView.frame.size.height); 
     self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x-widthSub,self.textLabel.frame.origin.y,self.textLabel.frame.size.width+widthSub,self.textLabel.frame.size.height); 
     self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x-widthSub,self.detailTextLabel.frame.origin.y,self.detailTextLabel.frame.size.width+widthSub,self.detailTextLabel.frame.size.height); 
     self.imageView.contentMode = UIViewContentModeScaleAspectFit; 
    } 
} 
@end 

... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[SizableImageCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    cell.textLabel.text = ... 
    cell.detailTextLabel.text = ... 
    cell.imageView.image = ... 
    return cell; 
} 
0

我已經修改別人的人的類別,以使所有的圖像佔據相同的寬度(可視寬度),無需縮放:

-(UIImage*) centerImage:(UIImage *)inImage inRect:(CGRect) thumbRect 
{ 

    CGSize size= thumbRect.size; 
    UIGraphicsBeginImageContext(size); 
    //calculation 
    [inImage drawInRect:CGRectMake((size.width-inImage.size.width)/2, (size.height-inImage.size.height)/2, inImage.size.width, inImage.size.height)]; 
    UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();   
    // pop the context 
    UIGraphicsEndImageContext(); 
    return newThumbnail; 
}