以编程方式调整表格单元格中的UIImage

时间:2011-12-02 07:27:16

标签: ios4

如何在表格单元格中调整UIImage的大小

        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 5.0; 
        cell.imageView.frame=CGRectMake(0, 0, 20, 30);
         cell.imageView.image =[UIImage imageNamed:@"Diseases.png"];

它不能正常工作

2 个答案:

答案 0 :(得分:2)

使用此方法,传递大小和图像,然后获取图像,然后在单元格中显示

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
return newImage;
}

您也可以制作自定义UITableViewCell,并根据需要进行设计

答案 1 :(得分:0)

这对我有用。

    /////////////////////////////////        TO RESIZE IMAGE                //////////////////////////////////

    //////////        FIRST WRITE DESIRED SIZE AND CALL BELOW FINCTION                ///////

CGSize newSize ;
newSize.width = 30;    
newSize.height = 30; 

UIImage *imgSelectedNew = [self imageWithImage:savedImage scaledToSize:newSize];



- (UIImage*)imageWithImage:(UIImage*)sourceImage scaledToSize:(CGSize)targetSize
{   

//UIImage *sourceImage = sourceImage;//= self;
UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;

CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;

CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0,0.0);

if (CGSizeEqualToSize(imageSize, targetSize) == NO) {

    CGFloat widthFactor = targetWidth / width;
    CGFloat heightFactor = targetHeight / height;

    if (widthFactor < heightFactor) 
        scaleFactor = widthFactor;
    else
        scaleFactor = heightFactor;

    scaledWidth  = width * scaleFactor;
    scaledHeight = height * scaleFactor;

    // center the image

    if (widthFactor < heightFactor) {
        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
    } else if (widthFactor > heightFactor) {
        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    }
}


// this is actually the interesting part:

UIGraphicsBeginImageContext(targetSize);

CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;

thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

if(newImage == nil) 
    NSLog(@"could not scale image");


return newImage ;

}
好吧,我也是n indorian,Gud看到另一个!!! :)