UIImageView适合宽度

时间:2012-03-27 16:54:43

标签: iphone objective-c ios cocoa-touch uiview

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

我尝试过使用UIViewContentModeScaleAspectFillUIViewContentModeScaleAspectFit,但我也没想过。

有什么想法吗?

感谢。

2 个答案:

答案 0 :(得分:10)

这是我写的一个图像类来做一些像这样的简单函数,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

答案 1 :(得分:4)

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

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