首页 > 代码库 > 相册、图库、拍照功能的访问以及图像角度设置
相册、图库、拍照功能的访问以及图像角度设置
在我们需要访问UIImagePickerController的时候,一般为选择图片,此时我们可能需要做如下操作:
一、访问相册、图库、拍照功能;
二、图片设置;
1、需要将选择的图片缩小 (等比例);
2、设置图片的形状 (比如:希望选择的图片为圆形);
3、将图片翻转一定的角度 (比如翻转90度、180度等)
以下是我自己的总结的一些方法:
一、访问功能
- (IBAction)changeIconAction:(UITapGestureRecognizer *)sender { // UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册",@"图库", nil]; // [actionSheet showInView:[UIApplication sharedApplication].keyWindow]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; [alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { dispatch_async(dispatch_get_main_queue(), ^{ [self makePhoto]; }); }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { dispatch_async(dispatch_get_main_queue(), ^{ [self choosePicture]; }); }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"图库" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { dispatch_async(dispatch_get_main_queue(), ^{ [self pictureLibrary]; }); }]]; [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } //跳转到imagePicker里 - (void)makePhoto{ pickerController = [[UIImagePickerController alloc] init]; pickerController.allowsEditing = YES; pickerController.delegate = self; pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:pickerController animated:YES completion:nil]; } //跳转到相册 - (void)choosePicture { pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; [self presentViewController:pickerController animated:YES completion:nil]; } //跳转图库 - (void)pictureLibrary { pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:pickerController animated:YES completion:nil]; } //用户取消退出picker时候调用 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { NSLog(@"%@",picker); [pickerController dismissViewControllerAnimated:YES completion:^{ }]; } //用户选中图片之后的回调 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSLog(@"%s,info == %@",__func__,info); UIImage *userImage = [self fixOrientation:[info objectForKey:@"UIImagePickerControllerOriginalImage"]]; userImage = [self scaleImage:userImage toScale:0.3]; //保存图片 // [self saveImage:userImage name:@"某个特定标示"]; [pickerController dismissViewControllerAnimated:YES completion:^{ }]; [self.headIcon setImage:userImage]; self.headIcon.contentMode = UIViewContentModeScaleAspectFill; self.headIcon.clipsToBounds = YES; //照片上传 [self upDateHeadIcon:userImage]; }
二、图片设置
1、图片缩放
1 /** 2 * 对图片进行缩放 (宽高缩放) 3 * 4 * @param image 图片原始对象 5 * @param scaleSize 缩放的倍数 6 * 7 * @return 缩放后的图片 8 */ 9 +(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{ 10 UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize)); 11 [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)]; 12 UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); 13 UIGraphicsEndImageContext(); 14 return scaledImage; 15 } 16 /** 17 * 对图片进行缩放 (容量大小缩至100k以内) 18 * 19 * @param image 图片对象 20 * @param scope 图像范围 (scope kb范围内) 21 * 22 * @return 处理后的图片对象 23 */ 24 +(UIImage *)originImage:(UIImage *)image withInScope:(int)scope{ 25 //头像 20k 广告 200k 26 27 NSData *data = http://www.mamicode.com/UIImageJPEGRepresentation(image, 1.0); 28 if (data.length > scope*1024) { 29 if (data.length > 1024*1024) { //1M以上 30 data = http://www.mamicode.com/UIImageJPEGRepresentation(image, 0.1); 31 } 32 else if (data.length > 512*1024) { //500kb - 1000kb 33 data = http://www.mamicode.com/UIImageJPEGRepresentation(image, 0.5); 34 } 35 else if (data.length > 200*1024) { //0.25M - 0.5M 36 data = http://www.mamicode.com/UIImageJPEGRepresentation(image, 0.9); 37 } 38 } 39 return [UIImage imageWithData:data]; 40 }
2、显示设置
2.1、图片合成(两张图片合成一张)
2.2、缩略图
3、设置水平方向转动角度
1 //修正照片方向(手机转90度方向拍照) 2 - (UIImage *)fixOrientation:(UIImage *)aImage { 3 4 // No-op if the orientation is already correct 5 if (aImage.imageOrientation == UIImageOrientationUp) 6 return aImage; 7 8 // We need to calculate the proper transformation to make the image upright. 9 // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. 10 CGAffineTransform transform = CGAffineTransformIdentity; 11 12 switch (aImage.imageOrientation) { 13 case UIImageOrientationDown: 14 case UIImageOrientationDownMirrored: 15 transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height); 16 transform = CGAffineTransformRotate(transform, M_PI); 17 break; 18 19 case UIImageOrientationLeft: 20 case UIImageOrientationLeftMirrored: 21 transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); 22 transform = CGAffineTransformRotate(transform, M_PI_2); 23 break; 24 25 case UIImageOrientationRight: 26 case UIImageOrientationRightMirrored: 27 transform = CGAffineTransformTranslate(transform, 0, aImage.size.height); 28 transform = CGAffineTransformRotate(transform, -M_PI_2); 29 break; 30 default: 31 break; 32 } 33 34 switch (aImage.imageOrientation) { 35 case UIImageOrientationUpMirrored: 36 case UIImageOrientationDownMirrored: 37 transform = CGAffineTransformTranslate(transform, aImage.size.width, 0); 38 transform = CGAffineTransformScale(transform, -1, 1); 39 break; 40 41 case UIImageOrientationLeftMirrored: 42 case UIImageOrientationRightMirrored: 43 transform = CGAffineTransformTranslate(transform, aImage.size.height, 0); 44 transform = CGAffineTransformScale(transform, -1, 1); 45 break; 46 default: 47 break; 48 } 49 50 // Now we draw the underlying CGImage into a new context, applying the transform 51 // calculated above. 52 CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height, 53 CGImageGetBitsPerComponent(aImage.CGImage), 0, 54 CGImageGetColorSpace(aImage.CGImage), 55 CGImageGetBitmapInfo(aImage.CGImage)); 56 CGContextConcatCTM(ctx, transform); 57 switch (aImage.imageOrientation) { 58 case UIImageOrientationLeft: 59 case UIImageOrientationLeftMirrored: 60 case UIImageOrientationRight: 61 case UIImageOrientationRightMirrored: 62 // Grr... 63 CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage); 64 break; 65 66 default: 67 CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage); 68 break; 69 } 70 71 // And now we just create a new UIImage from the drawing context 72 CGImageRef cgimg = CGBitmapContextCreateImage(ctx); 73 UIImage *img = [UIImage imageWithCGImage:cgimg]; 74 CGContextRelease(ctx); 75 CGImageRelease(cgimg); 76 return img; 77 }
相册、图库、拍照功能的访问以及图像角度设置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。