您可以在UIImage的添加類別和包括這種方法:
// Return the image, but with rounded corners. Useful for masking an image
// being used in a UITableView which has grouped style
- (UIImage *)imageWithRoundedCorners:(UIRectCorner)corners radius:(CGFloat)radius {
// We need to create a CGPath to set a clipping context
CGRect aRect = CGRectMake(0.f, 0.f, self.size.width, self.size.height);
CGPathRef clippingPath = [UIBezierPath bezierPathWithRoundedRect:aRect byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)].CGPath;
// Begin drawing
// Start a context with a scale of 0.0 uses the current device scale so that this doesn't unnecessarily drop resolution on a retina display.
// Use `UIGraphicsBeginImageContextWithOptions(aRect.size)` instead for pre-iOS 4 compatibility.
UIGraphicsBeginImageContextWithOptions(aRect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, clippingPath);
CGContextClip(context);
[self drawInRect:aRect];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return croppedImage;
}
然後,當你配置你的細胞,在表視圖控制器,調用是這樣的:
if (*SINGLE_ROW*) {
// We need to clip to both corners
cell.imageView.image = [image imageWithRoundedCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft) radius:radius];
} else if (indexPath.row == 0) {
cell.imageView.image = [image imageWithRoundedCorners:UIRectCornerTopLeft radius:radius];
} else if (indexPath.row == *NUMBER_OF_ITEMS* - 1) {
cell.imageView.image = [image imageWithRoundedCorners:UIRectCornerBottomLeft radius:radius];
} else {
cell.imageView.image = image;
}
但用真正的邏輯替換SINGLE_ROW等,以確定你在一節中是否有單行,或者它是最後一行。這裏需要注意的一件事是,我發現(實驗上)組風格表的半徑是12,它在模擬器中完美工作,但在iPhone上卻不是。我無法在非視網膜設備上進行測試。在iPhone 4上,半徑30看起來不錯(所以我想知道這是否是一個圖像縮放的事情,因爲我使用的圖像來自AddressBook,所以沒有隱含的縮放因子)。因此,我已經有一些代碼在此之前,修改半徑...
CGFloat radius = GroupStyleTableCellCornerRadius;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
// iPhone 4
radius = GroupStyleTableCellCornerRadiusForRetina;
}
希望幫助。
正如你所說的,它將圖像四捨五入,雖然沒有特別的幫助,但它確實使角落變圓了;) 因爲我懶得建立自己的剪輯區域,所以我只是將它製作成一個「純色「表。它比分組稍微有點吸引力,但它工作正常 – 2010-06-27 18:22:53