最近有點懶,又是很久沒有寫點東西,就連日記都是前兩個月才更新的。當然诗祸,找理由真的是我的強項。(哭著笑.臉)
先上效果圖:
中間隨便設置一個ImageView對象就行粪躬,然后直接放大招瑰排,一句話切換頭像。
代碼如下:
#import "ViewController.h"
#import "UIViewController+HeadFunction.h"
@interface ViewController ()
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
_imageView.center = self.view.center;
_imageView.image = [UIImage imageNamed:@"頭像"];
_imageView.layer.cornerRadius = _imageView.frame.size.width/2;
_imageView.layer.masksToBounds = YES;
[self.view addSubview:_imageView];
// 是的支鸡,你沒看錯冬念,就是這一句代碼
[self changeHeadImageWithHeadImageView:_imageView];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
// 判斷沙盒里是否有圖片文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@HeadImage.png",[userdefaults stringForKey:@"123"]]]; // 保存文件的名稱
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL result = [fileManager fileExistsAtPath:filePath];
if (result) {
_imageView.image = [UIImage imageWithContentsOfFile:filePath];
}
else {
_imageView.image = [UIImage imageNamed:@"頭像"];
}
}
@end
上面那個- (void)viewWillAppear:(BOOL)animated的代碼是為了加載出上一次運行時保存在沙盒里的圖片,如果沙盒沒有苍匆,就加載默認設置的刘急。
最重要的那句代碼就是這句啦:
** [self changeHeadImageWithHeadImageView:_imageView];**
我們只需要將ImageView這個對象傳進去即可。
首頁面
點擊那個ImageView浸踩。出現(xiàn)如下圖的彈框叔汁。彈框就是用的系統(tǒng)的,一個選擇是拍照調(diào)用相機检碗,一個是從相冊選擇据块,調(diào)用系統(tǒng)的相冊。也許大家會想折剃,只是寫了一句那樣的代碼怎么會有彈框出現(xiàn)的另假,是的,這句話里面點進去的那個類里必然是添加了手勢的怕犁,現(xiàn)在我們可以仔細看看那個一句代碼里的具體內(nèi)容边篮。
點擊彈框
彈框里的則是兩個回調(diào):
打開相機、打開相冊
里面的AvatarPictureViewController就是自定義的拍照界面奏甫。
#import "UIViewController+HeadFunction.h"
#import "UIViewController+ActionSheet.h"
#import <objc/runtime.h>
static const char *HeadManagerKey = "HeadManagerKey";
static const char *HeadPictureViewKey = "HeadPictureViewKey";
@implementation UIViewController (HeadFunction)
- (void)setHeadPictureView:(UIImageView *)headPictureView {
// 第一個參數(shù):給哪個對象添加關聯(lián)
// 第二個參數(shù):關聯(lián)的key戈轿,通過這個key獲取
// 第三個參數(shù):關聯(lián)的value
// 第四個參數(shù):關聯(lián)的策略
objc_setAssociatedObject(self, HeadPictureViewKey, headPictureView, OBJC_ASSOCIATION_ASSIGN);
}
- (UIImageView *)headPictureView {
return objc_getAssociatedObject(self, HeadPictureViewKey);
}
- (void)setHeadManager:(HeadManager *)headManager {
objc_setAssociatedObject(self, HeadManagerKey, headManager, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (HeadManager *)headManager {
return objc_getAssociatedObject(self, HeadManagerKey);
}
#pragma mark - ChangeHeadImage
- (void)changeHeadImageWithHeadImageView:(UIImageView *)headImageView{
self.headPictureView = headImageView;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headImageChange:)];
self.headPictureView.userInteractionEnabled = YES;
[self.headPictureView addGestureRecognizer:tap];
}
- (void)headImageChange:(UIGestureRecognizer *)tap{
NSNotificationCenter *notificationDefaultCenter = [NSNotificationCenter defaultCenter];
if (tap.state == UIGestureRecognizerStateEnded) {
self.headManager = [[HeadManager alloc] initWithTarget:self];
__weak __typeof(self) weakSelf = self;
self.headManager.cropImageBlock = ^(UIImage *cropImage){
weakSelf.headPictureView.image = cropImage;
[notificationDefaultCenter postNotificationName:headImageUpdateNotificationKey object:nil userInfo:@{headImageValue:cropImage}];
weakSelf.headManager = nil;
};
[self actionSheet:^{
[self.headManager checkCameraAvailability:^(BOOL auth) {
if (!auth) {
NSLog(@"沒有訪問相機權限");
return;
}
}];
[self.headManager openTakePhoto];
} ppBlock:^{
[self.headManager openPhotoalbums];
}];
}
}
@end
就是在這里加的tap手勢:嘻嘻~~~
一句代碼里的詳細內(nèi)容
對應的是在這個分類里面:
分類封裝
在上圖可以看出來里面的有個第三方,就是在網(wǎng)上看到的就直接拿過來用了阵子,然后自己再進行了第二次封裝思杯,最后就變成了一句代碼換了頭像。
在調(diào)用相冊里的時候挠进,可以在里面進行切換相冊色乾。
詳細代碼我放在<a >百度云盤</a> 提取密碼: 5vju
大家可以下載來自己玩L懿帷!暖璧!