【代码笔记】iOS-图片手势,上传照片
2016-06-13 09:14
387 查看
代码:
RootViewController.h
RootViewController.m
RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> { UIImageView *imageView; } @end
RootViewController.m
#import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title=@"图片手势,上传照片"; //初始化背景图 [self initBackgroundView]; } #pragma -mark -functions -(void)initBackgroundView { imageView =[[UIImageView alloc]initWithFrame:CGRectMake(100, 150, 200, 150)]; imageView.backgroundColor = [UIColor redColor]; [self.view addSubview:imageView]; [imageView setUserInteractionEnabled:YES]; UITapGestureRecognizer * clickGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickGesture:)]; [imageView addGestureRecognizer:clickGesture]; } #pragma -mark -doClickActions -(void)clickGesture:(UIGestureRecognizer*)gesture { UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图片来源" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"本地相册", nil]; [actionSheet showInView:self.view]; } #pragma -mark UIActionSheetDelegate -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"--buttonIndex-%ld",buttonIndex); UIImagePickerController *pickView = [[UIImagePickerController alloc]init]; pickView.delegate=self; if(buttonIndex==0&&!TARGET_IPHONE_SIMULATOR){ //相机 pickView.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:pickView animated:NO completion:nil]; }else if (buttonIndex==1){ //相册 pickView.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:pickView animated:NO completion:nil]; } } #pragma -mark -UIImagePickerControllerDelegate -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage]; [imageView setImage:image]; [picker dismissViewControllerAnimated:NO completion:nil]; } -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:NO completion:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
相关文章推荐
- iOS每日一记之————————SDwebimage图片的缓存机制
- iOS 跳转到应用设置界面
- iOS 摇一摇的功能
- iOS教你轻松打造瀑布流Layout
- iOS单例详解
- iOS之Runtime
- 监控服务之nagios服务
- iOS: SegmentControl 实现页面切换的四种方法
- iOS解析json的4种方法
- iOS 关于修饰代理用weak还是assign
- iOS多线程
- iOS 自带 MD5转化
- ios实现视频录制功能 一
- iOS开发中(null)与<null>的处理
- iOS消除对应的警告!
- ios,pod 更新版本和降级版本,OSX 10.10+,打包pod,全教程
- iOS中video标签播放视频
- iOS 导航栏透明,变色动画
- iOS开发之百度免费API使用案例(身份证号获取地址、手机号查归属地、MD5解密等)
- iOS开发大神必备的Xcode插件