//
// ViewController.m
// 访问系统相册
//
// Created by Adonis 16/7/12.
// Copyright © 2016年 LESSON 2. All rights reserved.
//
#import "ViewController.h"
// 遵守代理协议
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
// intterface后定义属性
@property(nonatomic,strong)UIButton *userBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 所有的可见控件创建初始化方式都可以采用alloc initWithFrame
self.userBtn = [[UIButton alloc] initWithFrame:CGRectMake(150, 60, 120, 120)];
// 设置控件颜色
// self.userBtn.backgroundColor = [UIColor blackColor];
// 导入初始企鹅图片
UIImage *image = [UIImage imageNamed:@"企鹅图片"];
// 将图片设置为userbtn初始界面
[self.userBtn setBackgroundImage:image forState:(UIControlStateNormal)];
// 设置圆形半径
self.userBtn.layer.cornerRadius = 60;
// 将正方形与圆形重合部分切割掉
self.userBtn.layer.masksToBounds = YES;
// 添加点击事件 去访问系统相册(setUserImage)
[self.userBtn addTarget:self action:@selector(setUserImage) forControlEvents:(UIControlEventTouchUpInside)];
// 添加到测试窗口
[self.view addSubview:self.userBtn];
}
//访问系统相册
-(void)setUserImage
{
// UIImagePickerController 图片选择控制器 访问相册和相机
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
// 设置代理,进delegate到@interface ViewController ()后遵守协议 加上<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
imagePicker.delegate = self;
// 相片弹出
[self presentViewController:imagePicker animated:YES completion:nil];
}
// 这个方法是UIImagePickerControllerDelegate协议中的,选择图片结束时就会自动调用图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{
// 设置头像
[self.userBtn setBackgroundImage:image forState:(UIControlStateNormal)];
// 收回系统相册 YES 为布尔型 需要过渡动画 nil为消失后无新事件
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
QQ登录界面(一)访问iPhone系统相册并更改图像
最新推荐文章于 2024-10-12 21:35:32 发布