• UIKit框架-高级控件:6.UIPickerView与UIImageView结合使用


    在前面, 我们用UIPickerView, UILabel, UITextFeild做了一个日期选择的小Demo, 现在让我们继续来看看UIPickerView和其他UI控件的结合.


    1.定义全局变量

    @interface ViewController ()
    {
        UILabel *_label;
        UIImageView *_imageView;
    }
    @end
    



    2.在.h文件里遵守代理方法

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
    @end
    


    3.添加UILabel

    - (void)myLabel
    {
        _label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 45, 20)];
        [_label setBackgroundColor:[UIColor greenColor]];
        [_label setText:@"头像: "];
        [self.view addSubview:_label];
    }
    


    4.添加UIImageView

    #pragma mark - UIImageView方法
    - (void)myImageView
    {
        CGFloat imageViewX = _label.frame.size.width + 30;
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageViewX, 28, 200, 200)];
        [_imageView setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:_imageView];
    }
    


    5.添加UIButton

    - (void)myButton
    {
        CGFloat buttonX = _label.frame.size.width + 30;
        CGFloat buutonY = _imageView.frame.size.height + 40;
        CGFloat buttonW = _imageView.frame.size.width;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setFrame:CGRectMake(buttonX, buutonY, buttonW, 30)];
        [button setTitle:@"选择头像按钮" forState:UIControlStateNormal];
        
        [button setBackgroundColor:[UIColor blackColor]];
        [button addTarget:self action:@selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }



    6.添加UIButton的监听方法

    - (void)selectPhoto
    {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        [imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        [imagePickerController setAllowsEditing:YES];
        [imagePickerController setDelegate:self];
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }


    7.添加ImagePikerController的代理方法

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        NSLog(@"%@", info);
        UIImage *image = info[@"UIImagePickerControllerEditedImage"];
        [_imageView setImage:image];
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    PS: UIImagePickerControllerEditedImage这个东西是在info里面打印出来的, 而info就是一个Dictionary, 里面有几个键值对, 例子中选择的是可以截取一部分图像添加到UIImageView中.



    8.在viewDidLoad中实现所有的方法

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self myLabel];
        [self myImageView];
        [self myButton];
    }
    




    最终的效果是:

      

     

     

    PS: 腾讯QQ里面选择头像也是这么做的.





    好了, 这次我们就讲到这里, 下次我们继续~~~

  • 相关阅读:
    MyBatis传入多个参数的问题
    vim添加复制(crtl+c),粘贴(ctrl+v)ctrl+A 等快捷键
    log4j日志级别
    概要设计说明书(转载自国家计算机标准和文件模板)
    软件需求分析文档模版(转载自国家计算机标准和文件模板)
    新概念英语 第二册 课文
    程序员的自我进化——补上最短的那块情商木板
    python 字符和数值转换
    maven中的 dependencies 和 dependencyManagement 的区别
    Oracle11G 7个服务说明
  • 原文地址:https://www.cnblogs.com/iOSCain/p/4333142.html
Copyright © 2020-2023  润新知