图像调整大小问题 - iPhone

时间:2012-01-05 07:32:45

标签: iphone uiimageview image-resizing

我的笔尖中有两个UIImageviews,其中一个是根据UISlider的值调整大小 代码是: -

-(IBAction)sliderChanged:(UISlider*)slider{
    CGFloat scaleFactor=slider.value;

    markerImage.transform = CGAffineTransformMakeScale(scaleFactor,scaleFactor);
}

正确调整大小,但是当我通过这些代码将这些图像合并在一起时

UIImage *backImage=backgroundImage.image;
UIImage *markImage=markerImage.image;

UIGraphicsBeginImageContext(backImage.size);

[backImage drawAtPoint:CGPointMake(0,0)];
[markImage drawAtPoint:CGPointMake(0,0)];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我合并了原始大小的图片, 也就是说,标记图像在结果图像中出现在相同的维度中 如何以正确的比例绘制标记图像 谢谢

1 个答案:

答案 0 :(得分:1)

而不是drawAtPoint:,请使用drawInRect:(CGRect)rect。此外,需要使用滑块值。

假设sliderValue介于0和1之间,

CGRect rect = CGRectMake(0,0, markerImage.size.width*sliderValue, markerImage.size.height*sliderValue);
[markImage drawInRect:rect];

修改

CGFloat width = markerImage.size.width*sliderValue;
CGFloat height = markerImage.size.height*sliderValue;

CGRect rect = CGRectMake((markerImage.size.width-width)/2,(markerImage.size.height-height)/2, width, height);
[markImage drawInRect:rect];