我们如何调整ios中的图像大小

时间:2015-10-14 07:14:44

标签: ios objective-c

嗨,我是Ios的初学者,在我的项目中,我在桌面列表上显示图像,所以一切都很好

但实际来自服务的图片有些太小,有些人会变得很大

我如何缩放图像以及如何适应它们

我的代码: -

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 45, 45)];
    imageView.image = [UIImage imageNamed:[Mainarray1  objectAtIndex:indexPath.row]];
    Cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [Cell.contentView addSubview:imageView];

    return Cell;
}

2 个答案:

答案 0 :(得分:0)

使用以下内容获取图像的原始尺寸和

imageView.contentMode=UIViewContentModeScaleAspectFit; 

在下面使用拉伸和拟合图像所需的任何尺寸:

imageView.contentMode=UIViewContentModeScaleAspectFill;

答案 1 :(得分:0)

调整图片大小。

- (UIImage *) generatePhotoThumbnail:(UIImage *)image1 withSide:(CGFloat)ratio
{
// Create a thumbnail version of the image for the event object.

CGSize size = image1.size;
CGSize croppedSize;

CGFloat offsetX = 0.0;
CGFloat offsetY = 0.0;

// check the size of the image, we want to make it
// a square with sides the size of the smallest dimension.
// So clip the extra portion from x or y coordinate

if (size.width > size.height) {
    offsetX = (size.height - size.width) / 2;
    croppedSize = CGSizeMake(size.height, size.height);
} else {
    offsetY = (size.width - size.height) / 2;
    croppedSize = CGSizeMake(size.width, size.width);
}

// Crop the image before resize
CGRect clippedRect = CGRectMake(offsetX * -1, offsetY * -1, croppedSize.width, croppedSize.height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image1 CGImage], clippedRect);
// Done cropping

// Resize the image
CGRect rect = CGRectMake(0.0, 0.0, ratio, ratio);

UIGraphicsBeginImageContext(rect.size);
[[UIImage imageWithCGImage:imageRef] drawInRect:rect];
UIImage *thumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// Done Resizing

return thumbnail;
}

设置图片

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

 imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 45, 45)];
imageView.image = [self generatePhotoThumbnail:[UIImage imageNamed:[Mainarray1  objectAtIndex:indexPath.row]] withSide:150.0];//;
Cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
[Cell.contentView addSubview:imageView];

return Cell;
}

希望它会对你有所帮助。