修改UIImagePicker编辑图片筐大小

本文介绍了一种处理图片编辑信息的方法,通过使用编辑信息来获取原始图片的裁剪区域。这种方法可以应用于从不同来源(如相册或相机)选择的图片,并能够处理图片的方向问题。文中提供了一个具体的实现示例,包括如何根据编辑信息调整裁剪区域并缩放图片。
This includes most of what you need, and takes care of all thecamera orientation issues. I've added the following which will takein the editing info and use it to get the original cropping rectwith this addition:
- (UIImage*)scaleImage:(UIImage*)anImagewithEditingInfo:(NSDictionary*)editInfo{

    UIImage*newImage;

    UIImage*originalImage = [editInfovalueForKey:@"UIImagePickerControllerOriginalImage"];
    CGSizeoriginalSize = CGSizeMake(originalImage.size.width,originalImage.size.height);
    CGRectoriginalFrame;
    originalFrame.origin= CGPointMake(0,0);
    originalFrame.size= originalSize;

    CGRectcroppingRect = [[editInfovalueForKey:@"UIImagePickerControllerCropRect"] CGRectValue];
    CGSizecroppingRectSize = CGSizeMake(croppingRect.size.width,croppingRect.size.height);

    CGSizecroppedScaledImageSize = anImage.size;

    floatscaledBarClipHeight = 80;

    CGSizescaledImageSize;
    floatscale;

    if(!CGSizeEqualToSize(croppedScaledImageSize,originalSize)){

        scale= croppedScaledImageSize.width/croppingRectSize.width;
        floatbarClipHeight = scaledBarClipHeight/scale;

        croppingRect.origin.y-= barClipHeight;
        croppingRect.size.height+= (2*barClipHeight);

        if(croppingRect.origin.y<=0){
            croppingRect.size.height+= croppingRect.origin.y;
            croppingRect.origin.y=0;
        }

        if(croppingRect.size.height> (originalSize.height -croppingRect.origin.y)){
            croppingRect.size.height= (originalSize.height - croppingRect.origin.y);
        }


        scaledImageSize= croppingRect.size;
        scaledImageSize.width*= scale;
        scaledImageSize.height*= scale;

        newImage=  [self cropImage:originalImageto:croppingRect andScaleTo:scaledImageSize];

    }else{

        newImage= originalImage;

    }

    returnnewImage;
}

I updated the call back method from the dev forums post to thefollowing:
复制代码
- (void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary*)editInfo {

    [selfdismissModalViewControllerAnimated:YES];
    self.myImageView.userInteractionEnabled=YES;
    CGRectimageFrame = myImageView.frame;
    CGPointimageCenter = myImageView.center;
    UIImage*croppedImage;


    NSMutableDictionary*imageDescriptor = [editInfo mutableCopy];

    //CGFloat scaleSize = 400.0f;
    CGFloatscaleSize = 640.0f;
    switch([picker sourceType]) {
            //done
        caseUIImagePickerControllerSourceTypePhotoLibrary:
            croppedImage= [self scaleImage:img withEditingInfo:editInfo];
            [imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
            break;


        caseUIImagePickerControllerSourceTypeCamera: {
            UIImageOrientationoriginalOrientation = [[editInfoobjectForKey:UIImagePickerControllerOriginalImage]imageOrientation];
            if(originalOrientation != UIImageOrientationUp) {
                NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];
                CGRectorigRect;
                [[editInfoobjectForKey:UIImagePickerControllerCropRect]getValue:&origRect];
                UIImage*rotatedImage = straightenAndScaleImage([editInfoobjectForKey:UIImagePickerControllerOriginalImage],scaleSize);
                CGFloatscale = scaleSize/1600.0f;
                origRect.origin.x*= scale;
                origRect.origin.y*= scale;
                origRect.size.width*= scale;
                origRect.size.height*= scale;
                croppedImage= [self cropImage:rotatedImage to:origRectandScaleTo:CGSizeMake(320, 480)];
                [imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
                [pooldrain];
            }
            else{
                croppedImage= [self scaleImage:img withEditingInfo:editInfo];
                [imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
            }
        }
            break;

        caseUIImagePickerControllerSourceTypeSavedPhotosAlbum: {
            UIImageOrientationoriginalOrientation = [[editInfoobjectForKey:UIImagePickerControllerOriginalImage]imageOrientation];
            if(originalOrientation != UIImageOrientationUp) {
                NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];
                CGRectorigRect;
                [[editInfoobjectForKey:UIImagePickerControllerCropRect]getValue:&origRect];
                UIImage*rotatedImage = straightenAndScaleImage([editInfoobjectForKey:UIImagePickerControllerOriginalImage],scaleSize);
                CGFloatscale = scaleSize/640.0f;
                origRect.origin.x*= scale;
                origRect.origin.y*= scale;
                origRect.size.width*= scale;
                origRect.size.height*= scale;
                croppedImage= [self cropImage:rotatedImage to:origRectandScaleTo:CGSizeMake(320, 480)];
                [imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
                [pooldrain];
            }
            else{
                croppedImage= [self scaleImage:img withEditingInfo:editInfo];
                [imageDescriptorsetObject:croppedImage forKey:@"croppedImage"];
            }
        }
            break;
        default:
            break;
    }

    imageFrame.size= croppedImage.size;
    myImageView.frame= imageFrame;
    myImageView.image= [imageDescriptor objectForKey:@"croppedImage"];
    myImageView.center= imageCenter;


}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值