我在 scrollView 中缩放图像,我想根据缩放调整图像大小,我知道我必须在scrollViewDidEndZooming方法中编写一些代码。
我正在关注这个链接,但我认为这没有更新(我在实现调整图像大小类别时出错),所以请以正确的方式指导我,如何根据缩放比例调整图像大小。
谢谢
#define DOC_WIDTH 505
#define DOC_HEIGHT 802
+(UIImage*)imageResizeWithBorderFromImage:(UIImage*)source
{
//******* Change size
int tnw;
int tnh;
double x_ratio = DOC_WIDTH / source.size.width;
double y_ratio = DOC_HEIGHT / source.size.height;
if (source.size.width <= DOC_WIDTH && source.size.height <= DOC_HEIGHT){
tnw=(int)source.size.width ;
tnh=(int)source.size.height;
}
else if ((x_ratio * source.size.height) < DOC_HEIGHT) {
tnh = (int)(ceil(x_ratio * source.size.height));
tnw = (int)DOC_WIDTH ;
}
else {
tnw = (int)(ceil(y_ratio * source.size.width));
tnh = (int)DOC_HEIGHT;
}
CGSize size = CGSizeMake(tnw, tnh);
//CGSize size = [source size];
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context,(0/255.f),(162/255.f),(237/255.f), 1.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}