iphone - UIImageView 适合宽度

标签 iphone objective-c ios cocoa-touch uiview

我有一个 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/

相关文章:

一直运行的 iOS GPS 跟踪应用程序

iphone - 设置动画总是出现

ios - 在开发设备上安装旧版 iPhone 操作系统

iphone - OpenAL 在循环播放声音时出现故障

ios - 从更新函数调用时,自定义 SKShapeNode 未出现在场景中

iphone - 将文件中的图像发布到 Facebook?

iphone - 如何构建 ilibjingle

objective-c - Swift 与 Objective-C 的性能对比

iOS:dismissViewControllerAnimated 在执行时崩溃

objective-c - 如何比较NSEvent的trackingArea来查看哪个trackingArea触发了 "theEvent"