UIImagePickerController

把手势添加到一个图片上, 别忘了开启*****用户交互****

self.photoImageView.userInteractionEnabled = YES;

 

//  RootViewController.m

//  imageHandle

//

//  Created by 李洪鹏 on 15/7/15.

//  Copyright (c) 2015 李洪鹏. All rights reserved.

//

 

#import "RootViewController.h"

#import "RootView.h"

 

@interface RootViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

 

@property (nonatomic, strong)RootView *rootView;

 

@end

 

@implementation RootViewController

 

-(void)loadView

{

    self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.view = self.rootView;

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //添加轻拍手势

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];

    

    [self.rootView.photoImageView addGestureRecognizer:tapGR];

 

}

 

//实现手势的方法

- (void)tapAction:(UITapGestureRecognizer *)sender

{

//    NSLog(@"大家好,我是手势");

    

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选取",@"拍照", nil];

    [sheet showInView:self.rootView];

    

}

 

#pragma mark- -----action的代理方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

    // 从相册中选择

    if (actionSheet.firstOtherButtonIndex == buttonIndex) {

        

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

            

            UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];

            

            // 通过代理方法拿到图片

            imagePC.delegate = self;

            

            // 可以对图片进行编辑,对于要拿到编辑的图片 这个属性必不可少

            imagePC.allowsEditing = YES;

            

            // 指定picker 从相册中去选取

            imagePC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

            

            [self presentViewController:imagePC animated:YES completion:nil];

        }

        

        

    } else if (buttonIndex == actionSheet.firstOtherButtonIndex + 1) { // 相机

        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

            

            UIImagePickerController *imagePC = [[UIImagePickerController alloc] init];

            

            // 通过代理方法拿到图片

            imagePC.delegate = self;

            

            // 可以对图片进行编辑,对于要拿到编辑的图片 这个属性必不可少

            imagePC.allowsEditing = YES;

            

            // 指定picker 去拍照

            imagePC.sourceType = UIImagePickerControllerSourceTypeCamera;

            

            [self presentViewController:imagePC animated:YES completion:nil];

            

        }

    }

    

    

}

 

 

//取消按钮的代理方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:nil];

    

}

 

#pragma mark-----实现代理方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

    [picker dismissViewControllerAnimated:YES completion:nil];

    //  (UIImagePickerControllerEditedImage) k值苹果文档已经写好了

    UIImage *image1 = [info objectForKey:UIImagePickerControllerEditedImage];

    

    self.rootView.photoImageView.image = image1;

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

/*

#pragma mark - Navigation

 

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

 

@end

原文地址:https://www.cnblogs.com/lhp-1992/p/4648443.html