版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.07.02 |
前言
在app中蝶押,我們經(jīng)常需要點(diǎn)擊別人分享發(fā)布的圖片或者頭像贾节,然后放大縮小等狞悲,還可以保存到本地相冊等撮抓。感興趣的可以看看我寫的其他小技巧。
1. 實(shí)用小技巧(一):UIScrollView中上下左右滾動方向的判斷
2. 實(shí)用小技巧(二):屏幕橫豎屏的判斷和相關(guān)邏輯
3.實(shí)用小技巧(三):點(diǎn)擊手勢屏蔽子視圖的響應(yīng)
4.實(shí)用小技巧(四):動態(tài)的增刪標(biāo)簽視圖
5.實(shí)用小技巧(五):通過相冊或者相機(jī)更改圖標(biāo)
6.實(shí)用小技巧(六):打印ios里所有字體
7. 實(shí)用小技巧(七):UITableViewCell自適應(yīng)行高的計(jì)算
8. 實(shí)用小技巧(八):數(shù)字余額顯示的分隔
9.實(shí)用小技巧(九):類頭條模糊背景的實(shí)現(xiàn)
10.實(shí)用小技巧(十):晃動手機(jī)換后臺服務(wù)器網(wǎng)絡(luò)
11.實(shí)用小技巧(十一):scrollView及其子類顯示的一些異常處理
功能需求
??我們項(xiàng)目中經(jīng)常需要有這樣的需求:需要點(diǎn)擊好友或者別人發(fā)布的圖片摇锋,并可捏合放大縮小圖片丹拯,最后還有保存到本地相冊的功能。
功能實(shí)現(xiàn)
下面我們就直接看代碼實(shí)現(xiàn)荸恕。
#import "JJAvatarSaveVC.h"
#import "Masonry.h"
@interface JJAvatarSaveVC ()
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, assign) double lastScale;
@end
@implementation JJAvatarSaveVC
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupUI];
}
- (void)setupUI
{
self.view.backgroundColor = [UIColor whiteColor];
//頭像
UIImageView *avatarImageView = [[UIImageView alloc] init];
avatarImageView.image = [UIImage imageNamed:@"sea"];
avatarImageView.userInteractionEnabled = YES;
avatarImageView.layer.cornerRadius = 50.0;
avatarImageView.layer.masksToBounds = YES;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureDidTapped)];
[avatarImageView addGestureRecognizer:tapGesture];
[self.view addSubview:avatarImageView];
self.avatarImageView = avatarImageView;
[self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.height.width.equalTo(@100);
}];
}
- (void)loadBackgroundView
{
UIView *backView = [[UIView alloc] initWithFrame:self.view.frame];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backViewTapGestureDidTapped)];
[backView addGestureRecognizer:tapGesture];
UIPinchGestureRecognizer *pinGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(imagePinGestureDidTapped:)];
[backView addGestureRecognizer:pinGesture];
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imageLongGestureDidTapped)];
[backView addGestureRecognizer:longGesture];
backView.backgroundColor = [UIColor blackColor];
[self.view addSubview:backView];
self.backView = backView;
CGSize imageSize = self.avatarImageView.image.size;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:@"sea"];
imageView.userInteractionEnabled = YES;
[backView addSubview:imageView];
self.imageView = imageView;
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = imageSize.height / imageSize.width * width;
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(backView);
make.width.equalTo(@(width));
make.height.equalTo(@(height));
}];
}
- (void)saveImageToAlbum
{
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo:(void *) contextInfo
{
NSString *msg = nil;
if (error != NULL) {
msg = @"保存圖片失敗";
}
else {
msg = @"保存圖片成功";
}
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ensureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.backView removeFromSuperview];
}];
[alertVC addAction:ensureAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
#pragma mark - Action && Notification
- (void)tapGestureDidTapped
{
self.navigationController.navigationBarHidden = YES;
[UIApplication sharedApplication].statusBarHidden = YES;
[self loadBackgroundView];
}
- (void)backViewTapGestureDidTapped
{
self.navigationController.navigationBarHidden = NO;
[UIApplication sharedApplication].statusBarHidden = NO;
[self.backView removeFromSuperview];
}
//捏合手勢放大縮小圖片
- (void)imagePinGestureDidTapped:(UIPinchGestureRecognizer *)sender
{
if([sender state] == UIGestureRecognizerStateEnded) {
self.lastScale = 1.0;
return;
}
CGFloat scale = 1.0 - (self.lastScale - sender.scale);
CGAffineTransform currentTransform = self.imageView.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
[self.imageView setTransform:newTransform];
self.lastScale = [sender scale];
}
//長按保存到相冊
- (void)imageLongGestureDidTapped
{
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *saveAction = [UIAlertAction actionWithTitle:@"保存到相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self saveImageToAlbum];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
}];
[alertVC addAction:saveAction];
[alertVC addAction:cancelAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
@end
功能效果
下面就運(yùn)行代碼乖酬,并查看結(jié)果。
可見實(shí)現(xiàn)了需求功能融求。
后記
未完咬像,待續(xù)~~~