2016年01月04日18:55:39 周一
今天我这边主要完成了”我” 的页面的基本布局和简单功能的实现.
涉及到的代码需要注意的部分
设置颜色的宏定义
//获取RGB颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0fgreen:g/255.0fblue:b/255.0falpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0fgreen:g/255.0fblue:b/255.0falpha:a]
#define RGB(r,g,b) RGBA(r,g,b,1.0f)
#define SKUIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue &0xFF0000)
>>16))/255.0green:((float)((rgbValue
&0xFF00) >>8))/255.0blue:((float)(rgbValue
&0xFF))/255.0alpha:1.0]
设置提示框的方式
第一种:
//通过弹窗提示用户切换头像
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"是否切换头像"message:nilpreferredStyle:(UIAlertControllerStyleActionSheet)];
UIAlertAction *camera = [UIAlertActionactionWithTitle:@"拍照"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction*_Nonnullaction) {
NSLog(@"调用系统相机");
//调用系统相机
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType= sourceType;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}
else{
NSLog(@"模拟器无法打开相机");
}
}];
UIAlertAction *photo = [UIAlertActionactionWithTitle:@"从相册中选择"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction*_Nonnullaction) {
NSLog(@"从相册中选择");
//调用系统相册
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate=self;
picker.allowsEditing=YES;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}];
UIAlertAction *cancle = [UIAlertActionactionWithTitle:@"取消"style:(UIAlertActionStyleCancel)handler:nil];
[alert addAction:camera];
[alert addAction:photo];
[alert addAction:cancle];
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:@"是否切换头像"message:nilpreferredStyle:(UIAlertControllerStyleActionSheet)];
UIAlertAction *camera = [UIAlertActionactionWithTitle:@"拍照"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction*_Nonnullaction) {
NSLog(@"调用系统相机");
//调用系统相机
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType= sourceType;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}
else{
NSLog(@"模拟器无法打开相机");
}
}];
UIAlertAction *photo = [UIAlertActionactionWithTitle:@"从相册中选择"style:(UIAlertActionStyleDefault)handler:^(UIAlertAction*_Nonnullaction) {
NSLog(@"从相册中选择");
//调用系统相册
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate=self;
picker.allowsEditing=YES;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}];
UIAlertAction *cancle = [UIAlertActionactionWithTitle:@"取消"style:(UIAlertActionStyleCancel)handler:nil];
[alert addAction:camera];
[alert addAction:photo];
[alert addAction:cancle];
[selfpresentViewController:alertanimated:YEScompletion:^{}];
第二种:
先设置属性
UIActionSheet*myActionSheet;
//下拉菜单
实现方法
//在这里呼出下方菜单按钮项
myActionSheet = [[UIActionSheetalloc]initWithTitle:@"是否切换头像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照",@"从相册选择",nil];
myActionSheet = [[UIActionSheetalloc]initWithTitle:@"是否切换头像"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"拍照",@"从相册选择",nil];
[myActionSheetshowInView:self.view];
注意代理 UIActionSheetDelegate
最后实现代理方法
- (void)actionSheet:(UIActionSheet*)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex
{
//呼出的菜单按钮点击后的响应
if (buttonIndex ==myActionSheet.cancelButtonIndex)
{
NSLog(@"取消");
}
switch (buttonIndex)
{
case 0: //打开照相机拍照
[selftakePhoto];
break;
case 1: //打开本地相册
[selfLocalPhoto];
break;
}
{
//呼出的菜单按钮点击后的响应
if (buttonIndex ==myActionSheet.cancelButtonIndex)
{
NSLog(@"取消");
}
switch (buttonIndex)
{
case 0: //打开照相机拍照
[selftakePhoto];
break;
case 1: //打开本地相册
[selfLocalPhoto];
break;
}
}
调用系统相机
实现方法
//调用系统相机
-(void)takePhoto
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *pickerCammer = [[UIImagePickerControlleralloc]init];
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
pickerCammer.sourceType=UIImagePickerControllerSourceTypeCamera;
pickerCammer.mediaTypes= [UIImagePickerControlleravailableMediaTypesForSourceType:pickerCammer.sourceType];
pickerCammer.delegate=self;
pickerCammer.allowsEditing=YES;
}
[selfpresentViewController:pickerCammeranimated:YEScompletion:nil];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
{
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *pickerCammer = [[UIImagePickerControlleralloc]init];
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
pickerCammer.sourceType=UIImagePickerControllerSourceTypeCamera;
pickerCammer.mediaTypes= [UIImagePickerControlleravailableMediaTypesForSourceType:pickerCammer.sourceType];
pickerCammer.delegate=self;
pickerCammer.allowsEditing=YES;
}
[selfpresentViewController:pickerCammeranimated:YEScompletion:nil];
}else
{
NSLog(@"模拟其中无法打开照相机,请在真机中使用");
}
}
//打开本地相册
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate=self;
//设置选择后的图片可被编辑
picker.allowsEditing=YES;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
-(void)LocalPhoto
{
UIImagePickerController *picker = [[UIImagePickerControlleralloc]init];
picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate=self;
//设置选择后的图片可被编辑
picker.allowsEditing=YES;
[selfpresentViewController:pickeranimated:YEScompletion:nil];
}
实现代理 UIImagePickerControllerDelegate
实现代理方法
#pragma mark -切换用户头像代理
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSString *type = [infoobjectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image,1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPathwithIntermediateDirectories:YESattributes:nilerror:nil];
[fileManager createFileAtPath:[DocumentsPathstringByAppendingString:@"/image.png"]contents:dataattributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSStringalloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
NSLog(@"图片的路径是:%@",filePath);
//关闭相册界面
[picker dismissViewControllerAnimated:YEScompletion:nil];
NSUserDefaults *userDefault = [NSUserDefaultsstandardUserDefaults];
[userDefault setObject:filePathforKey:@"userImage"];
[userDefault synchronize];
//将之前的按钮移除视图
[userImage_imageremoveFromSuperview];
[selfchooseUserImageAndTap];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker{
NSLog(@"您取消了选择图片");
[picker dismissViewControllerAnimated:YEScompletion:nil];
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
NSString *type = [infoobjectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil)
{
data = UIImageJPEGRepresentation(image,1.0);
}
else
{
data = UIImagePNGRepresentation(image);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory()stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManagerdefaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPathwithIntermediateDirectories:YESattributes:nilerror:nil];
[fileManager createFileAtPath:[DocumentsPathstringByAppendingString:@"/image.png"]contents:dataattributes:nil];
//得到选择后沙盒中图片的完整路径
filePath = [[NSStringalloc]initWithFormat:@"%@%@",DocumentsPath, @"/image.png"];
NSLog(@"图片的路径是:%@",filePath);
//关闭相册界面
[picker dismissViewControllerAnimated:YEScompletion:nil];
NSUserDefaults *userDefault = [NSUserDefaultsstandardUserDefaults];
[userDefault setObject:filePathforKey:@"userImage"];
[userDefault synchronize];
//将之前的按钮移除视图
[userImage_imageremoveFromSuperview];
[selfchooseUserImageAndTap];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker{
NSLog(@"您取消了选择图片");
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
本地化存储 NSUserDefault
获取到图片的本地路径后进行保存
NSUserDefaults*userDefault
= [NSUserDefaultsstandardUserDefaults];
[userDefault setObject:filePathforKey:@"userImage"];
[userDefault setObject:filePathforKey:@"userImage"];
[userDefaultsynchronize];
第二次登录的时候重新赋值
//从本地取之前保存的图片
NSUserDefaults *userDefault = [NSUserDefaultsstandardUserDefaults];
NSString *fileToUserImage = [userDefaultstringForKey:@"userImage"];
NSUserDefaults *userDefault = [NSUserDefaultsstandardUserDefaults];
NSString *fileToUserImage = [userDefaultstringForKey:@"userImage"];
image= [UIImageimageWithContentsOfFile:fileToUserImage];
需要注意
存储只能存储基本类型
NSUserDefaults支持的数据格式有:NSNumber(Integer、Float、Double),NSString,NSDate,NSArray,NSDictionary,BOOL类型
而且存储时,除NSNumber类型使用对应的类型意外,其他的都是使用setObject:forKey:
最后同步到硬盘 这步不是必须的,但是推荐加上 [userDefaultsynchronize];
使用 SVN 冲突的时候(SVN:155015)
1.自己手动查询(推荐)
2.可以使用这个按钮,但是很多时候会出错,毕竟自动合并
3.使用命令行解决(未验证实际效果)
或者
SVN resolve <你的文件夹名称>
更多精彩文章,尽在我的公众号.