美文网首页
系统相机拍照时,照片角度旋转的情况

系统相机拍照时,照片角度旋转的情况

作者: wbtuxi | 来源:发表于2018-06-09 15:28 被阅读32次
#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  // 系统的方法  回调
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    if ([type isEqualToString:( NSString *)kUTTypeImage]) {
        UIImage* image=[info objectForKey:UIImagePickerControllerOriginalImage];

        UIImageOrientation imageOrientation=image.imageOrientation;
        if(imageOrientation!=UIImageOrientationUp)
        {
            // 原始图片可以根据照相时的角度来显示,但UIImage无法判定,于是出现获取的图片会向左转90度的现象。
            // 以下为调整图片角度的部分
            UIGraphicsBeginImageContext(image.size);
            [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.width)];
            image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            // 调整图片角度完毕
        }
        [picker dismissViewControllerAnimated:YES completion:nil];

    }
}

正常调用相机

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic,strong) UIImagePickerController *picker;
//初始化picker
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.picker = [[UIImagePickerController alloc]init];
        self.picker.delegate = self;
        self.picker.allowsEditing =YES;//这句保证了图片可以裁剪为正方形
    });
#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    // 判断获取类型:图片
    if ([mediaType isEqualToString:( NSString *)kUTTypeImage]){
        UIImage *theImage = nil;
        // 判断,图片是否允许修改
        if ([picker allowsEditing]){
            //获取用户编辑之后的图像
            theImage = [info objectForKey:UIImagePickerControllerEditedImage];
        } else {
            // 照片的原数据
            theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
        }

        [picker dismissViewControllerAnimated:YES completion:nil];
    }
}

相关文章

网友评论

      本文标题:系统相机拍照时,照片角度旋转的情况

      本文链接:https://www.haomeiwen.com/subject/tsbteftx.html