我有一个安装在UIScrollView中的UIImageView。图像视图中的图像比iPhone的屏幕要大。我想要做的是让图像按比例缩放以适应屏幕的宽度,而不改变图像的纵横比(图像的一部分将在底部的屏幕外)。
我尝试过使用UIViewContentModeScaleAspectFill和UIViewContentModeScaleAspectFit,但它们都不能满足我的需要。
有什么想法吗?
谢谢。
发布于 2012-03-28 01:17:34
这是我写的一个图像类,用来做一些类似这样的简单函数,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;
@endMyImage.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发布于 2012-03-28 01:11:03
您必须以编程方式设置滚动视图的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];https://stackoverflow.com/questions/9894120
复制相似问题