在我的应用程序中,我使用了图像视图来显示从数据库获取的图像
数据库中的图像具有不同的大小,
现在我添加了带框架(10,10,300,220)的图像视图。
我需要根据图片大小调整图片视图的大小,就像 Aspect Fit.
我知道 Aspect fit能够调整大小但是根据高度比改变宽度,
但我需要根据宽度比增加高度
我需要宽度始终为300,已修复但需要更改高度
例如:
if image size is 500x1000 i need to resize imageview as 300x600
if image size is 400x600 i need to resize imageview as 300x450
答案 0 :(得分:3)
您可以使用以下符合要求的代码段
-(UIImage *)adjustImageSizeWhenCropping:(UIImage *)image
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float ratio=300/actualWidth;
actualHeight = actualHeight*ratio;
CGRect rect = CGRectMake(0.0, 0.0, 300, actualHeight);
// UIGraphicsBeginImageContext(rect.size);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 1.0);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
答案 1 :(得分:1)
resizedImage = [self imageWithImage:originalImage scaledToSize:CGSizeMake(45,45)];
self.imageView.image = resizedImage;
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}