首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIImageView适配宽度

UIImageView适配宽度
EN

Stack Overflow用户
提问于 2012-03-28 00:54:43
回答 2查看 5.6K关注 0票数 4

我有一个安装在UIScrollView中的UIImageView。图像视图中的图像比iPhone的屏幕要大。我想要做的是让图像按比例缩放以适应屏幕的宽度,而不改变图像的纵横比(图像的一部分将在底部的屏幕外)。

我尝试过使用UIViewContentModeScaleAspectFillUIViewContentModeScaleAspectFit,但它们都不能满足我的需要。

有什么想法吗?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-28 01:17:34

这是我写的一个图像类,用来做一些类似这样的简单函数,fitInsideWidth是应该在这里应用的函数:

MyImage.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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
票数 10
EN

Stack Overflow用户

发布于 2012-03-28 01:11:03

您必须以编程方式设置滚动视图的zoomScale

代码语言:javascript
复制
yourImageView = [[UIImageView alloc] initWithImage:yourImage];
[yourScrollView addSubview:self.imageView];
yourScrollview.contentSize = yourImageView.bounds.size;

[yourScrollView setZoomScale:yourScrollView.bounds.size.width/yourImageView.bounds.size.width animated:NO];
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9894120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档