这是实现图片缩放和居中显示的Demo:ScaleMoveImageDemo
程序参考自:http://blog.sina.com.cn/s/blog_9c3c519b0100za22.html
要实现图片的缩放和居中,我们需要用到UIScrollView来控制图片的缩放和居中显示(通过设置maximumZoomScale和minimumZoomScale两个属性来规定图片的最大最小比例,接着在scrollViewDidZoom函数中设置图片的居中显示)。
以下是一些重要的做法:
1、新建一个Empty Application工程testMoveScaleImage,记得打上Use Automatic Reference Counting 这个钩。
2、新建一个UIViewController,命名为MoveScaleImage。
在MoveScaleImage.h文件中,加入以下代码
@interface MoveScaleImage : UIViewController<UIScrollViewDelegate>
{
UIScrollView *myScrollView;
UIImageView* myImageView;
}
@property(retain,nonatomic)UIScrollView* myScrollView;
@property(retain,nonatomic)UIImageView* myImageView;
@end
在MoveScaleImage.h中,在开头部分加入
@synthesize myScrollView;
@synthesize myImageView;
接着在loadView函数中,我们要手动编程加入一个按钮,点击这个按钮将会触发按钮事件,该事件将会初始化在.h文件中定义的UIScrollView实例,并将其加入到self.view的子view中,该事件还会将ImageView加入到UIScrollView实例中,以此来响应图片的缩放和居中显示。具体如下:
loadView函数:
-(void)loadView{
[super loadView];
self.view.backgroundColor = [UIColor lightGrayColor];
// UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(110, 200, 100, 50)];
// [btn setFrame:CGRectMake(110, 200, 100, 40)];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn setTitle:@"点击查看图片" forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)clickEvent:(id)sender{
NSLog(@"***********clickeventad");
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
if(self.myScrollView==nil)
{
UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.myScrollView=scrollView;
}
self.myScrollView.backgroundColor=[UIColor blueColor];
self.myScrollView.delegate=self;
self.myScrollView.multipleTouchEnabled=YES; //是否支持多点触控
self.myScrollView.minimumZoomScale=1.0; //表示与原图片最小的比例
self.myScrollView.maximumZoomScale=10.0; //表示与原图片最大的比例
[self.view addSubview:self.myScrollView];
UIImage *_image = [UIImage imageNamed:@"QQ.png"];
CGFloat imageView_X = (_image.size.width > self.view.frame.size.width) ? self.view.frame.size.width : _image.size.width;
CGFloat imageView_Y=(_image.size.height > self.view.frame.size.height) ? self.view.frame.size.height : _image.size.height;
CGFloat origin;
//保持图片的比例
if(_image.size.width > self.view.frame.size.width){
origin = self.view.frame.size.width/_image.size.width;
imageView_Y = _image.size.height*origin;
}
myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-imageView_X)/2, (self.view.frame.size.height-imageView_Y)/2, imageView_X, imageView_Y)];
if(self.myImageView==nil)
{
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
self.myImageView=imageView;
}
UIImage *originImage=[[UIImage alloc]initWithCGImage:_image.CGImage];
[myImageView setImage:originImage];
// [myImageView setFrame:CGRectMake(0, 0, _image.size.width, _image.size.height)];
[self.myScrollView addSubview:self.myImageView];
//返回按钮
UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[closeBtn setBackgroundColor:[UIColor redColor]];
[closeBtn setTitle:@"返回" forState:UIControlStateNormal];
[closeBtn setAlpha:0.5];
[closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:closeBtn];
}
//按下该按钮将隐藏myImageView和myScrollView
-(void)closeEvent:(id)sender{
[self.myImageView setHidden:YES];
[self.myScrollView setHidden:YES];
}
//实现图片在缩放过程中居中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
self.myImageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}
//返回缩放的是哪个ImageView
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
NSLog(@"**************viewForZoomingInScrollView");
return self.myImageView;
}
以上就是MoveScaleImage的实现部分。
最后记得在AppDelegate.m头部加入:
#import "MoveScaleImage.h"
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MoveScaleImage* moveScaleImage=[[MoveScaleImage alloc] init];
self.window.rootViewController=moveScaleImage; //记得加这行代码,否则会出错
[self.window addSubview:moveScaleImage.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;