1 .头像上传:
1.头像上传的是每一个app都会使用的的东西,这里总结下头像的上传的东西东西,去选选择相册或者拍照的东西如下代码:
.h的代码:
#import
#import"BaseViewController.h"
typedefvoid(^photoBlock)(UIImage*photo);
@interfaceBaseViewController (XHPhoto)
/**
*照片选择->图库/相机
*@param edit照片是否需要裁剪,默认NO
*@param block照片回调
*/
-(void)showCanEdit:(BOOL)edit photo:(photoBlock)block;
.m的代码:
#import"BaseViewController+XHPhoto.h"
#import"objc/runtime.h"
#import
#ifdef DEBUG
#define debugLog(...)NSLog(__VA_ARGS__)
#else
#define debugLog(...)
#endif
staticBOOLcanEdit =NO;
staticcharblockKey;
@interfaceBaseViewController()
@property(nonatomic,copy)photoBlockphotoBlock;
@end
@implementationBaseViewController (XHPhoto)
#pragma mark-set
-(void)setPhotoBlock:(photoBlock)photoBlock
{
objc_setAssociatedObject(self, &blockKey, photoBlock,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#pragma mark-get
- (photoBlock)photoBlock
{
returnobjc_getAssociatedObject(self, &blockKey);
}
-(void)showCanEdit:(BOOL)edit photo:(photoBlock)block
{
if(edit)canEdit= edit;
self.photoBlock= [blockcopy];
UIActionSheet*sheet= [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"相册中获取",nil];
sheet.tag=2599;
[sheetshowInView:self.view];
}
#pragma mark - action sheet delegte
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(actionSheet.tag==2599)
{
//权限
ALAuthorizationStatusauthor = [ALAssetsLibraryauthorizationStatus];
if(author ==ALAuthorizationStatusRestricted|| author ==ALAuthorizationStatusDenied) {
NSString*photoType = buttonIndex==0?@"相机":@"相册";
NSString* title = [NSStringstringWithFormat:@"%@权限未开启",photoType];
NSString* msg = [NSStringstringWithFormat:@"请在系统设置中开启该应用%@服务\n(设置->隐私->%@->开启)",photoType,photoType];
NSString* cancelTitle =@"知道了";
UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:titlemessage:msgdelegate:selfcancelButtonTitle:cancelTitleotherButtonTitles:nil,nil];
[alertViewshow];
debugLog(@"%@权限未开启",photoType);
return;
}
//跳转到相机/相册页面
UIImagePickerController* imagePickerController = [[UIImagePickerControlleralloc]init];
imagePickerController.delegate=self;
imagePickerController.allowsEditing=canEdit;
switch(buttonIndex)
{
case0:
//拍照
//是否支持相机
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePickerController.sourceType=UIImagePickerControllerSourceTypeCamera;
[selfpresentViewController:imagePickerControlleranimated:YEScompletion:NULL];
}
else
{
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"温馨提示"message:@"该设备不支持相机"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alertshow];
}
break;
case1:
//相册
imagePickerController.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
[selfpresentViewController:imagePickerControlleranimated:YEScompletion:NULL];
default:
break;
}
}
}
#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[pickerdismissViewControllerAnimated:YEScompletion:^{}];
UIImage*image;
//是否要裁剪
if([pickerallowsEditing]){
//编辑之后的图像
image = [infoobjectForKey:UIImagePickerControllerEditedImage];
}else{
image = [infoobjectForKey:UIImagePickerControllerOriginalImage];
}
if(self.photoBlock)
{
self.photoBlock(image);
}
}
/*
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(BaseViewController *)viewController animated:(BOOL)animated {
if ([navigationController isKindOfClass:[UIImagePickerController class]] &&
((UIImagePickerController *)navigationController).sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}
}
*/
@end