iOS相册图片的选取与设置

原创 2015年11月17日 16:36:26

我的模块中常常用户的头像可以用户自定义自己的头像,可以选择拍照也可以选择从用户的自己的相册中选择,具体的实现代码如下:

@interface PickerViewController : UIViewController<UIImagePickerControllerDelegate,UIActionSheetDelegate,UINavigationControllerDelegate>

//名字
@property (weak, nonatomic) IBOutlet UILabel *namelabel;
//头像
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
//actionsheet底部弹窗
@property (nonatomic,strong) UIActionSheet *actionSheet;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.namelabel.text = @"XXX";
    self.iconView.image = [UIImage imageNamed:@"IMG_1743"];
    self.iconView.layer.masksToBounds = YES;
    self.iconView.layer.cornerRadius = 40;
    self.iconView.userInteractionEnabled = YES;
    //给头像添加点击手势
    UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
    [self.iconView addGestureRecognizer:ges];
- (void)tapClick {
    NSLog(@"打开相册");
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选取", nil];
    }else {
        self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",nil];
    }
    self.actionSheet.tag = 1000;
    [self.actionSheet showInView:self.view];
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (actionSheet.tag == 1000) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    //来源:相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                case 1:
                    //来源:相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
                case 2:
                    return;
            }
        }
        else {
            if (buttonIndex == 2) {
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:^{
            
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.iconView.image = image;
//    self.namelabel.text = image.description;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

iOS9中,Swift判断相机,相册权限,选取图片为头像

在iOS7以后要打开手机摄像头或者相册的话都需要权限,在iOS9中更是更新了相册相关api的调用 首先新建一个swift工程,在SB中放上一个按钮,并在viewController中拖出点击事件 ok...

iOS 之摄像头/相册选取图片

iphone中图像通常存储在4个地方【相册、应用程序包、沙盒、Internet】,通过这4个源,我们就可以存取应用图片。 相册      iphone的相册包含摄像头胶卷+用户计算机同步的部分...

Android设置头像,手机拍照或从本地相册选取图片作为头像

【Android设置头像,手机拍照或从本地相册选取图片作为头像】 像微信、QQ、微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1,让用户通过选择本地相册之类的图片库中已有...

Android设置头像,手机拍照或从本地相册选取图片作为头像

 【Android设置头像,手机拍照或从本地相册选取图片作为头像】 像微信、QQ、微博等社交类的APP,通常都有设置头像的功能,设置头像通常有两种方式: 1,让用户通过选择本地相册之类的图片...

iOS图片加水印效果的实现并保存至相册

图片加水印效果的实现并保存至相册

IOS 相册图片选择组件

  • 2015-08-01 21:56
  • 673KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)