iOS 從圖庫(kù)選取圖片,顯示在imageView上碳锈,點(diǎn)擊imageView可以查看大圖顽冶,可以刪除

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:@"拍照",@"手機(jī)相冊(cè)",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:@"設(shè)備不支持拍照"];

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末售碳,一起剝皮案震驚了整個(gè)濱河市强重,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贸人,老刑警劉巖间景,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異艺智,居然都是意外死亡倘要,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門十拣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)封拧,“玉大人,你說(shuō)我怎么就攤上這事夭问≡笪鳎” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵甲喝,是天一觀的道長(zhǎng)尝苇。 經(jīng)常有香客問(wèn)我铛只,道長(zhǎng),這世上最難降的妖魔是什么糠溜? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任淳玩,我火速辦了婚禮,結(jié)果婚禮上非竿,老公的妹妹穿的比我還像新娘蜕着。我一直安慰自己,他們只是感情好红柱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布承匣。 她就那樣靜靜地躺著,像睡著了一般锤悄。 火紅的嫁衣襯著肌膚如雪韧骗。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天零聚,我揣著相機(jī)與錄音袍暴,去河邊找鬼。 笑死隶症,一個(gè)胖子當(dāng)著我的面吹牛政模,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蚂会,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼淋样,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了胁住?” 一聲冷哼從身側(cè)響起趁猴,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎措嵌,沒(méi)想到半個(gè)月后躲叼,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡企巢,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年枫慷,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浪规。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡或听,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出笋婿,到底是詐尸還是另有隱情誉裆,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布缸濒,位于F島的核電站足丢,受9級(jí)特大地震影響粱腻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜斩跌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一绍些、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧耀鸦,春花似錦柬批、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至洛姑,卻和暖如春上沐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背吏口。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工奄容, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人产徊。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像蜀细,于是被迫代替她去往敵國(guó)和親舟铜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容