设为首页 加入收藏

TOP

【代码笔记】iOS-图片手势,上传照片
2017-10-13 10:23:39 】 浏览:6347
Tags:代码 笔记 iOS- 图片 手势 上传 照片

代码:

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 摇一摇的功能 下一篇APP上架被拒经历:图标和名称侵权

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目