IOS手指控制图片的缩放

举报
清雨小竹 发表于 2022/09/25 00:05:34 2022/09/25
【摘要】 //// MoveScaleImageController.h// MoveScaleImage//// Created by on 12-4-24.// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.// #import <UIKit/UIKit....

  
  1. //
  2. // MoveScaleImageController.h
  3. // MoveScaleImage
  4. //
  5. // Created by on 12-4-24.
  6. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "MoveScaleImageView.h"
  10. @interface MoveScaleImageController : UIViewController<UIScrollViewDelegate>{
  11. UIScrollView *myScrollView;
  12. UIImageView *myImageView;
  13. }
  14. @property(retain,nonatomic)UIScrollView *myScrollView;
  15. @property(retain,nonatomic)UIImageView *myImageView;
  16. @end
  17. //
  18. // MoveScaleImageController.m
  19. // MoveScaleImage
  20. //
  21. // Created by on 12-4-24.
  22. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
  23. //
  24. #import "MoveScaleImageController.h"
  25. @interface MoveScaleImageController ()
  26. @end
  27. @implementation MoveScaleImageController
  28. @synthesize myScrollView;
  29. @synthesize myImageView;
  30. -(void)dealloc{
  31. [myScrollView release];
  32. [myImageView release];
  33. [super dealloc];
  34. }
  35. -(void)loadView{
  36. [super loadView];
  37. self.view.backgroundColor = [UIColor lightGrayColor];
  38. // UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  39. UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(110, 200, 100, 50)];
  40. // [btn setFrame:CGRectMake(110, 200, 100, 40)];
  41. [btn setBackgroundColor:[UIColor whiteColor]];
  42. [btn setTitle:@"点击查看图片" forState:UIControlStateNormal];
  43. [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
  44. [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  45. [btn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
  46. [self.view addSubview:btn];
  47. [btn release];
  48. //下面是我要剪切区域的覆盖层
  49. // if(self.centerOverLayView==nil)
  50. // {
  51. // UIView *centerView=[[UIView alloc] initWithFrame:CGRectMake(20, 100, 280, 210)];
  52. // self.centerOverLayView=centerView;
  53. // [centerView release];
  54. // }
  55. // self.centerOverLayView.backgroundColor=[UIColor clearColor];
  56. // self.centerOverLayView.layer.borderColor=[UIColor orangeColor].CGColor;
  57. // self.centerOverLayView.layer.borderWidth=2.0;
  58. // [self.view addSubview:self.centerOverLayView];
  59. }
  60. -(void)clickEvent:(id)sender{
  61. NSLog(@"***********clickeventad");
  62. myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  63. if(self.myScrollView==nil)
  64. {
  65. UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  66. self.myScrollView=scrollView;
  67. [scrollView release];
  68. }
  69. self.myScrollView.backgroundColor=[UIColor blueColor];
  70. self.myScrollView.delegate=self;
  71. self.myScrollView.multipleTouchEnabled=YES;
  72. self.myScrollView.minimumZoomScale=1.0;
  73. self.myScrollView.maximumZoomScale=10.0;
  74. [self.view addSubview:self.myScrollView];
  75. UIImage *_image = [UIImage imageNamed:@"image.jpg"];
  76. CGFloat imageView_X = (_image.size.width > self.view.frame.size.width) ? self.view.frame.size.width : _image.size.width;
  77. CGFloat imageView_Y;
  78. CGFloat origin;
  79. if(_image.size.width > self.view.frame.size.width){
  80. origin = self.view.frame.size.width/_image.size.width;
  81. imageView_Y = _image.size.height*origin;
  82. }
  83. 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)];
  84. if(self.myImageView==nil)
  85. {
  86. UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  87. self.myImageView=imageView;
  88. [imageView release];
  89. }
  90. // [myImageView setImage:_image];
  91. UIImage *originImage=[[UIImage alloc]initWithCGImage:_image.CGImage];
  92. [myImageView setImage:originImage];
  93. // [myImageView setFrame:CGRectMake(0, 0, _image.size.width, _image.size.height)];
  94. [self.myScrollView addSubview:self.myImageView];
  95. UIButton *closeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
  96. [closeBtn setBackgroundColor:[UIColor redColor]];
  97. [closeBtn setAlpha:0.5];
  98. [closeBtn addTarget:self action:@selector(closeEvent:) forControlEvents:UIControlEventTouchUpInside];
  99. [self.view addSubview:closeBtn];
  100. [closeBtn release];
  101. // UIView *backView = [[UIView alloc] initWithFrame:CGRectInset(self.view.frame, 5, 5)];
  102. // backView.alpha = 0.5;
  103. // backView.backgroundColor = [UIColor blackColor];
  104. [self.view addSubview:backView];
  105. //
  106. // UIImage* image=[UIImage imageNamed:@"image.jpg"];
  107. // MoveScaleImageView*fileContent = [[MoveScaleImageView alloc]initWithFrame:CGRectMake(0, 44, 320, 436)];
  108. // [fileContent setImage:image];
  109. //
  110. [backView addSubview:fileContent];
  111. // [self.view addSubview:fileContent];
  112. //
  113. // [backView release];
  114. // [fileContent release];
  115. }
  116. -(void)closeEvent:(id)sender{
  117. [self.myImageView setHidden:YES];
  118. [self.myScrollView setHidden:YES];
  119. }
  120. #pragma mark UIScrollView delegate methods
  121. //实现图片的缩放
  122. -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
  123. NSLog(@"**************viewForZoomingInScrollView");
  124. return self.myImageView;
  125. }
  126. //实现图片在素芳过程中居中
  127. - (void)scrollViewDidZoom:(UIScrollView *)scrollView
  128. {
  129. CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
  130. CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
  131. self.myImageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
  132. }
  133. - (void)viewDidLoad
  134. {
  135. [super viewDidLoad];
  136. // Do any additional setup after loading the view.
  137. }
  138. - (void)viewDidUnload
  139. {
  140. [super viewDidUnload];
  141. // Release any retained subviews of the main view.
  142. }
  143. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  144. {
  145. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  146. }
  147. @end

文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/8448114

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。