UIKit框架-高级控件:6.UIPickerView与UIImageView结合使用
2015-03-02 19:03
435 查看
在前面, 我们用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里面选择头像也是这么做的.
好了, 这次我们就讲到这里, 下次我们继续~~~
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里面选择头像也是这么做的.
好了, 这次我们就讲到这里, 下次我们继续~~~
相关文章推荐
- UIKit框架-高级控件:8.UIPickerView, UILabel, UIImageView结合使用
- UIKit框架-高级控件:7.UIPickerView的自定义
- UIKit框架-高级控件:4.UIDatePickerView的基本认识
- UIKit框架-高级控件:9.UIPickerView简易城市地区选择器
- UIKit框架-高级控件Swift版本: 1.UIScrollView方法/属性详解
- UIKit框架-高级视图-UITableView-表格控件
- UIKit框架-高级控件:1.UIScrollView的基本认识
- UIKit框架-高级控件:3.UIScrollView的多图分页设置
- iOS开发UI高级—10使用picker View控件完成一个简单的选餐应用
- UIKit框架-高级视图-UICollectionView-表格控件
- UIKit框架-高级控件Swift版本: 2.UITableView方法/属性详解
- UIKit框架-高级控件Swift版本: 6.UIAlertView方法/属性详解
- UIKit框架-高级控件:10.UITableView的简单简介
- UIKit框架-高级控件:2.UIScrollView的单图分页设置
- UIKit框架-高级控件Swift版本: 4.UICollectionView方法/属性详解
- Android 下拉刷新控件SwipeRefreshLayout结合WebView使用
- Android 下拉刷新控件SwipeRefreshLayout结合WebView使用
- 02-常见控件:UILabel(显示文本)、UIImageView(图片视图)、UISlider(滑动器)、UISwitch(开关)、UIStepper(计数器)、UIToolbar(工具栏)的使用
- iOS学习笔记—— UIPickerView 控件的简单使用
- iOS开发之六:常用控件--UIImageView的使用