Iphone UIImageView适配宽度

Iphone UIImageView适配宽度,iphone,objective-c,ios,cocoa-touch,uiview,Iphone,Objective C,Ios,Cocoa Touch,Uiview,我有一个UIImageView,它位于UIScrollView中。图像视图中的图像比iPhone屏幕大。我想做的是让图像按比例缩放,以适应屏幕的宽度,而不改变图像的纵横比(图像的一部分将在屏幕底部) 我尝试过使用UIViewContentModeScaleAspectFill和UIViewContentModeScaleAspectFit,但都没有达到我想要的效果 有什么想法吗 谢谢。您必须以编程方式设置滚动视图的缩放比例: yourImageView = [[UIImageView alloc

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


我尝试过使用
UIViewContentModeScaleAspectFill
UIViewContentModeScaleAspectFit
,但都没有达到我想要的效果

有什么想法吗


谢谢。

您必须以编程方式设置滚动视图的
缩放比例

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];

下面是我编写的一个image类,用于执行一些简单的函数,例如,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

UIViewContentModeScaleAspectFit
应该做您想做的事情,前提是图像视图的框架也符合您的需要。tipycalFlow是正确的,您是否尝试过使UIImageView的高度非常大?如果它与实际图像一样高或更高,那么它应该可以工作。谢谢!这很有帮助。我可以用你的一些算法来计算高度。
#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