博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS实现图片的缩放和居中显示
阅读量:6230 次
发布时间:2019-06-21

本文共 5720 字,大约阅读时间需要 19 分钟。

直接上代码

//

//  MoveScaleImageController.h

//  MoveScaleImage

//

//  Created by  on 12-4-24.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import <UIKit/UIKit.h>

#import "MoveScaleImageView.h"

@interface MoveScaleImageController : UIViewController<UIScrollViewDelegate>{

    UIScrollView *myScrollView;

    UIImageView *myImageView;

}

@property(retain,nonatomic)UIScrollView *myScrollView;

@property(retain,nonatomic)UIImageView *myImageView;

@end

 

//

//  MoveScaleImageController.m

//  MoveScaleImage

//

//  Created by  on 12-4-24.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//


#import "MoveScaleImageController.h"


@interface MoveScaleImageController ()


@end


@implementation MoveScaleImageController

@synthesize myScrollView;

@synthesize myImageView;


-(void)dealloc{

    [myScrollView release];

    [myImageView release];

    [super dealloc];

}


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

    [btn release];

    

    //下面是我要剪切区域的覆盖层

//    if(self.centerOverLayView==nil)

//    {

//        UIView *centerView=[[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 210)];

//        self.centerOverLayView=centerView;

//        [centerView release];

//    }

//    self.centerOverLayView.backgroundColor=[UIColor clearColor];

//    self.centerOverLayView.layer.borderColor=[UIColor orangeColor].CGColor;

//    self.centerOverLayView.layer.borderWidth=2.0;

//    [self.view addSubview:self.centerOverLayView];

    

}


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

        [scrollView release];

    }

    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:@"image.jpg"];

    CGFloat imageView_X = (_image.size.width > self.view.frame.size.width) ? self.view.frame.size.width : _image.size.width;

    CGFloat imageView_Y;

    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;

        [imageView release];

    }

   

//    [myImageView setImage:_image];

    

    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 setAlpha:0.5];

    [closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:closeBtn];

    [closeBtn release];

    

//    UIView *backView = [[UIView alloc] initWithFrame:CGRectInset(self.view.frame, 5, 5)];

//    backView.alpha = 0.5;

//    backView.backgroundColor = [UIColor blackColor];

    [self.view addSubview:backView];

//    

//    UIImage* image=[UIImage imageNamed:@"image.jpg"];

//    MoveScaleImageView*fileContent = [[MoveScaleImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];

//    [fileContent setImage:image];

//    

    [backView addSubview:fileContent];

//    [self.view addSubview:fileContent];

//    

//    [backView release];

//    [fileContent release];

}


-(void)closeEvent:(id)sender{

    [self.myImageView setHidden:YES];

    [self.myScrollView setHidden:YES];

}


#pragma mark UIScrollView delegate methods

//实现图片的缩放

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{

    NSLog(@"**************viewForZoomingInScrollView");

    return self.myImageView;

}

//实现图片在缩放过程中居中

- (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);

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}


@end

本文转自 卓行天下  51CTO博客,原文链接:http://blog.51cto.com/9951038/1772523,如需转载请自行联系原作者
你可能感兴趣的文章
mysql qadir_MySQL 及 SQL 注入
查看>>
python 播放视频 ftp_视频在通过Python ftplib上传时出现损坏
查看>>
python3 image_python docker快速入门3制作image
查看>>
python代码写名字_【python】【名字】是什么?,Pythonname
查看>>
python调用浏览器生成dom_1分钟快速生成用于网页内容提取的xslt
查看>>
tolua unity 报错_关于LUA+Unity开发_toLua篇【二】
查看>>
java this函数_java this 用法详解
查看>>
java怎么封装表单数据_java 对form表单数据进行封装list
查看>>
java 当前工作目录是指_java取得当前工作目录
查看>>
the java jive_Java Jive
查看>>
hadoop上传文件java_hadoop入门之通过java代码实现将本地文件上传到hadoop的文件系统...
查看>>
放苹果 java_用Java代码模拟实现:一个人不断往箱子里放苹果,另一个人不
查看>>
java ftp 判断目录存在_java判断ftp目录是否存在的方法
查看>>
java httpclient 进度条_如何获得一个文件的上传与Apache HttpClient的4进度条
查看>>
java获取指定日期的后一天_java获得指定日期的前一天,后一天的代码详解
查看>>
java反射工具类_反射和BeanUtils工具类的使用
查看>>
mysql概念模型中的3种基本联系_《数据库基础》练习题
查看>>
java vips_Java IConfigManager.getAllVIPs方法代碼示例
查看>>
java c 转换_由javac完成的任何类型的转换?
查看>>
java properties类_Java Properties 类
查看>>