给图片添加手势,上传照片_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 给图片添加手势,上传照片

给图片添加手势,上传照片

 2014/10/10 16:57:37  粉粉色  程序员俱乐部  我要评论(0)
  • 摘要:.h#import<UIKit/UIKit.h>@interfaceRootViewController:UIViewController<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>{UIImageView*imageView;}.m-(void)viewDidLoad{[superviewDidLoad]
  • 标签:图片 上传

 

.h

 

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImageView *imageView;
}

 

.m

 

- (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-%i",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];
    
}

 

发表评论
用户名: 匿名