2016.8.31
//
// oneViewController.m
// test
//
// Created by lijie on 16/7/21.
// Copyright © 2016年AD. All rights reserved.
//
#import"oneViewController.h"
@interfaceoneViewController()
@property(weak,nonatomic)IBOutletUIImageView*picView;//
@property(weak,nonatomic)UIScrollView*scrollView;
@property(weak,nonatomic)UIImageView*lastImageView;
@property(nonatomic,assign)CGRectoriginalFrame;
@property(nonatomic,assign)BOOLisDoubleTap;
@property(nonatomic,strong)UITapGestureRecognizer*tapBg;
@end
@implementationoneViewController
- (void)viewDidAppear:(BOOL)animated {
if(self.picView.image) {
self.picView.userInteractionEnabled=YES;
UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(lookPic:)];
[self.picViewaddGestureRecognizer:tap];
}else{
self.picView.userInteractionEnabled=NO;
}
}
- (void)viewDidLoad {
[superviewDidLoad];
self.picView.layer.borderColor= [[UIColororangeColor]CGColor];
self.picView.layer.borderWidth=1;
self.picView.layer.masksToBounds=YES;
//让图片不被拉伸或压缩,且不超出imageView显示图片的一部分
self.picView.contentMode=UIViewContentModeScaleAspectFill;
self.picView.clipsToBounds=YES;
}
- (IBAction)addPic:(id)sender {
UIActionSheet*sheet = [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"手机相册",nil];
[sheetshowInView:self.view];
}
#pragma mark - actionsheet
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if(buttonIndex ==0|| buttonIndex ==1) {
UIImagePickerController*picker = [[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
if(buttonIndex ==0) {
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType=UIImagePickerControllerSourceTypeCamera;
}else{
// [self showFailure:@"设备不支持拍照"];
return;
}
}else{
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
}else{
return;
}
}
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}else{
return;
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker {//取消图片选择
[pickerdismissViewControllerAnimated:YEScompletion:nil];
}
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {//选择图片
UIImage*image = info[UIImagePickerControllerEditedImage];//获取图片
self.picView.image= image;
[pickerdismissViewControllerAnimated:YEScompletion:nil];
}
- (void)lookPic:(UITapGestureRecognizer*)tap {
//scrollView作为背景
UIScrollView*bgView = [[UIScrollViewalloc]init];
bgView.frame= [UIScreenmainScreen].bounds;
bgView.backgroundColor= [UIColorblackColor];
self.tapBg= [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapBgView:)];
[bgViewaddGestureRecognizer:self.tapBg];
UIButton*delete = [UIButtonbuttonWithType:UIButtonTypeCustom];
delete.frame=CGRectMake(20,20,30,30);
[deletesetImage:[UIImageimageNamed:@"trent_delete"]forState:UIControlStateNormal];
[deleteaddTarget:selfaction:@selector(deletePic)forControlEvents:UIControlEventTouchUpInside];
[bgViewaddSubview:delete];
UIImageView*picView = (UIImageView*)tap.view;
UIImageView*imageView = [[UIImageViewalloc]init];
imageView.image= picView.image;
imageView.frame= [bgViewconvertRect:picView.framefromView:self.view];
[bgViewaddSubview:imageView];
[[[UIApplicationsharedApplication]keyWindow]addSubview:bgView];
self.lastImageView= imageView;
self.originalFrame= imageView.frame;
self.scrollView= bgView;
//最大放大比例
self.scrollView.maximumZoomScale=1.5;
self.scrollView.delegate=self;
[UIViewanimateWithDuration:0.5animations:^{
CGRectframe = imageView.frame;
frame.size.width= bgView.frame.size.width;
frame.size.height= frame.size.width* (imageView.image.size.height/ imageView.image.size.width);
frame.origin.x=0;
frame.origin.y= (bgView.frame.size.height- frame.size.height) *0.5;
imageView.frame= frame;
}];
}
-(void)tapBgView:(UITapGestureRecognizer*)tapBgRecognizer {
self.scrollView.contentOffset=CGPointZero;
[UIViewanimateWithDuration:0.5animations:^{
self.lastImageView.frame=self.originalFrame;
tapBgRecognizer.view.backgroundColor= [UIColorclearColor];
}completion:^(BOOLfinished) {
[tapBgRecognizer.viewremoveFromSuperview];
self.scrollView=nil;
self.lastImageView=nil;
}];
}
//返回可缩放的视图
-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView {
returnself.lastImageView;
}
- (void)deletePic {
self.lastImageView.image=nil;
self.picView.image=nil;
[selftapBgView:self.tapBg];
}
@end