调整UIImage ios的大小

时间:2014-03-11 05:21:36

标签: ios uiimage size

我在UITableView JSON中添加了图片(只是通过了网址),现在我想调整图片的大小,我想在主页面中调整它们的大小,如缩略图。谢谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    // Configure the cell...
    City * cityObject;
    cityObject = [ citiesArry objectAtIndex:indexPath.row];
    cell.textLabel.text = cityObject.cityState;
    cell.detailTextLabel.text = cityObject.cityName;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailTextLabel.numberOfLines = 4;
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cityObject.cityImage]  ];
    [[cell imageView] setImage:[UIImage imageWithData:imageData]  ];
    return cell;
}

3 个答案:

答案 0 :(得分:1)

.m文件的顶部:

#define MySize CGSizeMake(100, 100) // any size that you need

然后在UIImage或任何您喜欢的地方制作类别方法:

+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
UIGraphicsEndImageContext();
return newImage;
}

然后你的cellForRowAtIndexPath看起来像:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
City * cityObject;
cityObject = [ citiesArry objectAtIndex:indexPath.row];
cell.textLabel.text = cityObject.cityState;
cell.detailTextLabel.text = cityObject.cityName;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.numberOfLines = 4;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cityObject.cityImage]  ];
UIImage *image = [UIImage imageWithData:imageData];
image = [UIImage imageWithImage:image scaledToSize:MySize];
[[cell imageView] setImage:image];
return cell;
}

除此之外,强烈建议不要在主线程上使用[NSData dataWithContentsOfURL: ];。您应该在辅助线程上提前获取数据

答案 1 :(得分:0)

您可以使用以下内容:

 + (UIImage *)scaleImage:(NSData *)imageData toSize:(CGSize)size {
    UIImage *image = [UIImage imageWithData:imageData];
    CGFloat scale = [[UIScreen mainScreen]scale];
    UIGraphicsBeginImageContextWithOptions(size, NO, scale);
    [image drawInRect:CGRectMake(0,0,size.width,size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

我只是将UIImage子类化并将其添加为类方法。希望它有所帮助

答案 2 :(得分:0)

如果要缩放图像,可以使用CoreGraphics函数

- (UIImage*)scaleImage:(UIImage*)image toSize:(CGSize)newSize {
    CGSize scaledSize = newSize;
    float scaleFactor = 1.0;
    if( image.size.width > image.size.height ) {
        scaleFactor = image.size.width / image.size.height;
        scaledSize.width = newSize.width;
        scaledSize.height = newSize.height / scaleFactor;
    }
    else {
        scaleFactor = image.size.height / image.size.width;
        scaledSize.height = newSize.height;
        scaledSize.width = newSize.width / scaleFactor;
    }

    UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
    CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
    [image drawInRect:scaledImageRect];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();    

    return scaledImage;
}