直接上代码

//

//  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

转载自:http://blog.sina.com.cn/s/blog_9c3c519b0100za22.html

iOS实现图片的缩放和居中显示的更多相关文章

  1. ios 改变图片大小缩放方法

    http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/39089858 ...

  2. NPOI 图片在单元格等比缩放且居中显示

    NPOI导出的图片默认是在单元格左上方,这使得图片在单元格显示得很难看.居中,且等比缩放,才是图片在单元格上的完美展示. /// <summary> /// 图片在单元格等比缩放居中显示 ...

  3. CSS图片Img等比例缩放且居中显示

    常用来做图片放大显示的遮罩层imgScale HTML <div id="imgScale" > <img src=""> </d ...

  4. 用<center/>标签实现markdown 图片文字等内容居中显示

    markdown中,文字居中的方式是借助了html标签<center></center>的支持 示例 ![](https://img2018.cnblogs.com/blog/ ...

  5. 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示

    我们都知道在ios中,每一个UIImageView都有他的frame大小,但是如果图片的大小和这个frame的大小不符合的时候会怎么样呢?在默认情况,图片会被压缩或者拉伸以填满整个区域. 通过查看UI ...

  6. Android ImageView的几种对图片的缩放处理 解决imageview放大图片后失真问题解决办法

    我的解决办法: 1 首先设置android:layout_width=”wrap_content”和android:layout_height=”wrap_content”,否则你按比例缩放后的图片放 ...

  7. js控制 固定框架内图片 按比例显示 以及 占满框架 居中显示

    js控制 固定框架内图片 等比例显示 以及 占满框架 纵横居中显示 通过设置 js函数 fitDiv里面var fit的值就好 function fitDiv (obj) { var target_w ...

  8. 纯JS实现图片预览与等比例缩放和居中

    最近做项目时有一个需求,广告位图片上传时要预览,并且要等比例缩放和居中.已经保存的广告位图片显示时也要等比例缩放和居中.我使用了下面的代码实现,不过可能有一些小问题. <!DOCTYPE HTM ...

  9. css 图片内容在不同分辨率下居中显示(演示的图片宽度是1920px,当图片宽度大于显示屏的宽度时)

    1.img 图片内容在不同分辨率下居中显示(如果隐藏多余,在img外面套一个div  设定overflow: hidden.div的大小就是img显示区域的大小) <!DOCTYPE html& ...

随机推荐

  1. react-redux-react-router直通车

    简单说明 这篇文章是我学习react一个多月来的总结,从基础开始(包括编辑器设置,构建工具搭建),一步一步走向react开发.相信我,看完这篇文章,跟着文章的步骤走,保证让你入门react并爱上rea ...

  2. 【阿里云产品公测】在Laravel4框架中使用阿里云OCS缓存

    作者:阿里云用户 supechina Laravel 是我最近用得非常多而且越用就越喜欢的一款PHP框架,由于没有向下兼容的历史包袱,完全面向对象的风格,借助 Facades 优雅的 IoC Cont ...

  3. Android小项目之二 代码的组织结构

    ------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...

  4. 《Cortex-M0权威指南》之体系结构---程序映像和启动流程

    转载请注明来源:cuixiaolei的技术博客 我们先来看看程序映像. 通常,Cortex-M0处理器的程序映像时从地址0x00000000处开始的. 程序映像开始处时向量表,其中包含了异常的其实地址 ...

  5. ASP.NET页面与IIS底层交互和工作原理详解(第二回)

    引言 在 Part.1 Http请求处理流程 一文中,我们了解了Http请求的处理过程以及其它一些运作原理.我们知道Http管道中有两个可用接口,一个是IHttpHandler,一个是IHttpMod ...

  6. 转 : React Native 开发之 IDE 选型和配置

    转:https://mp.weixin.qq.com/s?__biz=MzA3ODg4MDk0Ng==&mid=2651112392&idx=1&sn=135e29ddde30 ...

  7. Unity之定时调用

    1.Invoke(string methodName,float time) 在一定时间调用methodName函数 using UnityEngine; using System.Collectio ...

  8. Mac 在命令行快速切换目录 mark

    转自: http://www.ccvita.com/520.html ,略修改. 每天在命令行下,一大部分的工作都是一遍又一遍的输入 cd ~/some/very/deep/often-used/di ...

  9. VC++ DLL 发布到生产环境过程

    最近项目中用到了VC++ DLL,在本机调试时无任何问题,但是发布出来后,COPY到另外的机器就报错,说找不到DLL,由于自身工作接触这方面比较少,经过一番折腾后,终于解决,以下为解决步骤 一,平台工 ...

  10. mvc的一些知识点

    MVC是微软2009对外公布的第一个开源的表示层框架,这是微软的第一个开源项目, MVC目的不是取代WebFrom开发,只是web开发的另外一种选择 1.MVC设计模式 M:Model 主要是存储或者 ...