ios 图片居中裁剪_iOS实现图片的缩放和居中显示

该博客介绍了如何在iOS应用中实现图片的居中裁剪和缩放功能。通过创建一个UIScrollView和UIImageView,设置视图控制器属性,当点击按钮时加载图片并调整其在ScrollView中的位置和大小,以实现图片的居中显示和缩放操作。同时,提供了关闭功能,允许用户关闭显示的图片。
摘要由CSDN通过智能技术生成

//

// MoveScaleImageController.h

// MoveScaleImage

//

// Created by on 12-4-24.

// Copyright (c) 2012年 __MyCompanyName__. All

rights reserved.

//

#import

#import "MoveScaleImageView.h"

@interface MoveScaleImageController:

UIViewController{

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值