模仿微信更改头像,图片局部放大

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#import "ViewController.h"

@interface ViewController (){
UIImageView *showImageView;
UIImageView *imageView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorgrayColor];
//女孩图片
imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(70, 50,241, 360)];
// imageView.layer.contents = (__bridge id)([UIImage imageNamed:@"图片0.jpg"].CGImage);
imageView.image = [UIImageimageNamed:@"图片0.jpg"];
imageView.alpha =0.4;
imageView.userInteractionEnabled =YES;
[self.viewaddSubview:imageView];

//显示放大的局部图片
UIImageView *screenShotView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 80,80)];
//screenShotView.backgroundColor = [UIColor yellowColor];
screenShotView.backgroundColor = [UIColorclearColor];
screenShotView.userInteractionEnabled =YES;
screenShotView.layer.borderColor = [UIColorredColor].CGColor;
screenShotView.layer.borderWidth =1.0f;
[imageViewaddSubview:screenShotView];
// 手势拖动
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizeralloc]initWithTarget:self action:@selector(panGestureRecognizer:)];
[screenShotView addGestureRecognizer:panGesture];
showImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100,440, 190,190)];
showImageView.backgroundColor = [UIColorredColor];
[self.viewaddSubview:showImageView];
// Do any additional setup after loading the view, typically from a nib.

}

//移动手势的回调方法
- (void)panGestureRecognizer:(UIPanGestureRecognizer*)panGesture{
CGPoint point = [panGesturelocationInView:imageView];
panGesture.view.center = point;
CGRect rect = panGesture.view.frame;
UIImage *newImage = [selfInterceptionUIImageWithImage: imageView.image andRect:rect];
showImageView.image = newImage;
// ((UIImageView*)panGesture.view).image = newImage;
}
//截取图片的某一部分
- (UIImage*)InterceptionUIImageWithImage:(UIImage*)image andRect:(CGRect)rect{
CGImageRef imageRef = image.CGImage;
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *imageRect = [[UIImagealloc] initWithCGImage:imageRefRect];
return imageRect;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

效果

图片名称

图片名称

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 代码
  2. 2. 效果