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

Ios4 以编程方式调整表格单元格中UIImage的大小,ios4,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"]; 它不工

如何在表格单元格中调整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"];

它不工作

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

+ (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,并根据需要进行设计

这对我很有用

    /////////////////////////////////        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 ;

}
//调整图像大小//////////////////////////////////
//////////首先写下所需的大小,然后调用下面的函数///////
CGSize;
newSize.width=30;
newSize.height=30;
UIImage*imgSelectedNew=[self-imageWithImage:savedImage-scaledToSize:newSize];
-(UIImage*)图像WithImage:(UIImage*)源图像缩放大小:(CGSize)目标大小
{   
//UIImage*sourceImage=sourceImage;//=self;
UIImage*newImage=nil;
CGSize imageSize=sourceImage.size;
CGFloat width=imageSize.width;
CGFloat height=图像大小.height;
CGFloat targetWidth=targetSize.width;
CGFloat targetHeight=targetSize.height;
CGFloat scaleFactor=0.0;
CGFloat scaledWidth=targetWidth;
CGFloat缩放高度=目标高度;
CGPoint thumbnailPoint=CGPointMake(0.0,0.0);
if(CGSizeEqualToSize(imageSize,targetSize)==否){
CGFloat宽度系数=目标宽度/宽度;
CGFloat高度系数=目标高度/高度;
if(宽度系数<高度系数)
scaleFactor=宽度因子;
其他的
比例因子=高度因子;
scaledWidth=宽度*缩放因子;
缩放高度=高度*缩放因子;
//将图像居中
if(宽度系数<高度系数){
thumbnailPoint.y=(targetLight-缩放高度)*0.5;
}否则如果(宽度因子>高度因子){
thumbnailPoint.x=(targetWidth-缩放宽度)*0.5;
}
}
//这实际上是有趣的部分:
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect=CGRectZero;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width=缩放宽度;
thumbnailRect.size.height=缩放高度;
[sourceImage drawInRect:thumbnailRect];
newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsSendImageContext();
if(newImage==nil)
NSLog(@“无法缩放图像”);
返回新图像;
}
嗯,我也是印度裔,很高兴看到另一个!!!:)