如何调整Uiimage的高宽比

时间:2011-06-21 07:50:41

标签: ipad

我正在开展一个项目,我必须裁剪图像并保存照片库中的裁剪图像。我已经成功裁剪图像,但问题是,当我裁剪图像并显示它时,它被拉伸。我也使用宽高比模式。但它仍然很紧张。这是代码

-(void)cropLassoArea {

isLassoSelected = NO;
lassoStyle = 0;
CGRect frame;
frame = mainImageView.frame;
mainImageView.frame.size = CroppedImage.size;
for (int i = 0; i < [lassoArray count]; i++) {

    CAShapeLayer *layer = [lassoArray objectAtIndex:i];

    CGPathRef myPath = layer.path;
   UIGraphicsBeginImageContext(mainImageView.frame.size);

    [mainImageView setContentMode:UIViewContentModeCenter];


   [mainImageView.image drawInRect:CGRectMake(0, 0,CroppedImage.size.width,CroppedImage.size.height)]; 

    context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextAddPath(context, myPath);

    CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), [UIColor whiteColor].CGColor);
    CGContextSetBlendMode(context, kCGBlendModeClear);
    CGContextDrawPath(context, kCGPathFill);
    CGContextEOFillPath(context);

    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    CGContextSaveGState(context);
    UIGraphicsEndImageContext();

}

}

1 个答案:

答案 0 :(得分:0)

我建议您从this位置下载文件,我建议您阅读他们的教程。它很容易,根本不会拉伸你的图像。我在我的一个工作项目中使用过这些文件。您只需要导入其头文件并调用以下行

    UIImage *tmpImage = [yourImageView.image resizedImage:CGSizeMake(desiredWidth,DesiredHeight) interpolationQuality:kCGInterpolationHigh];
    yourImageView.contentMode = UIViewContentModeCenter;
    [yourImageView setImage:tmpImage];

希望它有所帮助。