打开本地相册或打开本地相机拍照,获取图片裁剪图片,上传图片
加入头文件
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
- 1
- 2
- 3
- 1
- 2
- 3
全部变量
NSString*THE_IMG_PATH;
NSString *_imageString
- 1
- 2
- 1
- 2
添加代理
<VPImageCropperDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
- 1
- 1
#pragma mark 用户头像点击响应的方法,
- (void)getImageFrom{
UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册中选取", nil];
[choiceSheet showInView:self.view];
}
#pragma mark UIActionSheetDelegate 点击ActionSheet 选择头像来源相册或者相机
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// 来源相机-拍照
NSString *mediaType = AVMediaTypeVideo;
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
if(authStatus == ALAuthorizationStatusRestricted ||authStatus == ALAuthorizationStatusDenied){
//不能访问相机
NSLog(@"相机权限受限");
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"访问相机失败" message:@"请打开 设置-隐私-相机 来进行设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
return;
}else{
//设置可以访问相机
if ([UIImagePickerOrCamera isCameraAvailable] && [UIImagePickerOrCamera doesCameraSupportTakingPhotos]) {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerOrCamera isFrontCameraAvailable]) {
controller.cameraDevice =UIImagePickerControllerCameraDeviceFront;
}
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
controller.mediaTypes = mediaTypes;
controller.delegate = self;
[self presentViewController:controller animated:YES completion:^(void){
NSLog(@"Picker View Controller is presented");
}];
}
}
} else if (buttonIndex == 1) {
//来源相册——访问相册
ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied) {
//设置不能访问相册
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"访问相册失败" message:@"请打开 设置-隐私-照片 来进行设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
}else{
//设置可以访问相册
if ([UIImagePickerOrCamera isPhotoLibraryAvailable]) {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
[mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
controller.mediaTypes = mediaTypes;
controller.delegate = self;
[self presentViewController:controller animated:YES completion:^(void){
NSLog(@"Picker View Controller is presented");
}];
}
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
#pragma mark - 相册导航器的 cancle的响应,退出相册
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:^(){
//TO Do
}];
}
//对相册的导航条修改
#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
if ([navigationController isKindOfClass:[UIImagePickerController class]] &&((UIImagePickerController *)navigationController).sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
}
}
#pragma mark 设置相册导航控制器的 导航模式
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
-
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
//裁剪
#pragma mark UIImagePickerControllerDelegate 裁剪图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//相册退出
[picker dismissViewControllerAnimated:YES completion:^() {
UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
portraitImg = [UIImagePickerOrCamera imageByScalingToMaxSize:portraitImg];
// 裁剪
VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];
imgEditorVC.delegate = self;
[self presentViewController:imgEditorVC animated:YES completion:^{
// TO DO
}];
}];
}
#pragma mark 取消裁剪
- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
[cropperViewController dismissViewControllerAnimated:YES completion:^{
}];
}
#pragma mark VPImageCropperDelegate 裁剪完成,对图片压缩处理
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
//存储在本地
THE_IMG_PATH=[UIImagePickerOrCamera saveImage:editedImage WithName:@"userHeadImage.png"];
//上传小图片,对图片尺寸压缩
editedImage=[UIImagePickerOrCamera imageWithImage:editedImage scaledToSize:CGSizeMake(Main_Screen_Width, Main_Screen_Width)];
//图片Gzip压缩
NSData*imageData=UIImagePNGRepresentation(editedImage);
//可以对图片进行gzip压缩或者其他相应处理
_imageString=[[NSString alloc]initWithData:imageData encoding:NSUTF8StringEncoding];
/**
*/
[cropperViewController dismissViewControllerAnimated:YES completion:^{
// TO DO
/**
图片裁剪完成,可以做相应处理
*/
}];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
图片的上传 这里是用第三方AFNetWorking
-(viod)updata{
UIApplication *application = [UIApplication sharedApplication];
AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
manager.requestSerializer.timeoutInterval = 5;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/javascript",@"text/html", @"application/json",nil];
NSArray*arrar=[THE_IMG_PATH componentsSeparatedByString:@"/"];
NSString*imageName=[arrar objectAtIndex:arrar.count-1];
NSMutableDictionary*dic=@{@"image":_imageString,@"imagePath":imageName};
[manager POST:urlString parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
if(sucess){
sucess(responseObject);
NSLog(@"responseObject=%@",responseObject);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"LYYB----AFNetWoring.errno=%@",error);
fails(error);
}];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25