gpt4 book ai didi

iphone - UIImageView 适合宽度

转载 作者:可可西里 更新时间:2023-11-01 05:02:58 24 4
gpt4 key购买 nike

我有一个 UIImageView,它位于 UIScrollView 中。 ImageView 中的图像比 iPhone 的屏幕大。我想要做的是按比例缩放图像以适应屏幕宽度,而不改变图像的纵横比(部分图像将在底部脱离屏幕)。

我试过使用 UIViewContentModeScaleAspectFillUIViewContentModeScaleAspectFit 但都没有达到我想要的效果。

有什么想法吗?

谢谢。

最佳答案

这是我编写的一个图像类来执行一些像这样的简单功能,fitInsideWidth 是应该在此处应用的函数:

MyImage.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MyImage : NSObject

+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width covertToHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image convertToHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width;
+ (UIImage*)imageWithImage:(UIImage *)image fitInsideWidth:(float)width fitInsideHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image fitOutsideWidth:(float)width fitOutsideHeight:(float)height;
+ (UIImage*)imageWithImage:(UIImage *)image cropToWidth:(float)width cropToHeight:(float)height;

@end

MyImage.m

#import "MyImage.h"

@implementation MyImage

+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width covertToHeight:(float)height {
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}

+ (UIImage*)imageWithImage:(UIImage *)image convertToHeight:(float)height {
float ratio = image.size.height / height;
float width = image.size.width / ratio;
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}

+ (UIImage*)imageWithImage:(UIImage *)image convertToWidth:(float)width {
float ratio = image.size.width / width;
float height = image.size.height / ratio;
CGSize size = CGSizeMake(width, height);
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage * newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimage;
}

+ (UIImage*)imageWithImage:(UIImage *)image fitInsideWidth:(float)width fitInsideHeight:(float)height {
if (image.size.height >= image.size.width) {
return [MyImage imageWithImage:image convertToWidth:width];
} else {
return [MyImage imageWithImage:image convertToHeight:height];
}
}

+ (UIImage*)imageWithImage:(UIImage *)image fitOutsideWidth:(float)width fitOutsideHeight:(float)height {
if (image.size.height >= image.size.width) {
return [MyImage imageWithImage:image convertToHeight:height];
} else {
return [MyImage imageWithImage:image convertToWidth:width];
}
}

+ (UIImage*)imageWithImage:(UIImage *)image cropToWidth:(float)width cropToHeight:(float)height {
CGSize size = [image size];
CGRect rect = CGRectMake(((size.width-width) / 2.0f), ((size.height-height) / 2.0f), width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage * img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return img;
}

@end

关于iphone - UIImageView 适合宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9894120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com