修改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;


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值