缩放图像并重新计算(x,y)

时间:2014-09-03 20:57:39

标签: ios objective-c iphone transform scale

我有一张尺寸为11900x3200的图像,上面标有一个点(2745,2730)。此尺寸太大,无法在iPhone屏幕上显示。我想将图像缩小到3200x2000。如何计算新缩小图像上的新(x,y)位置。

1 个答案:

答案 0 :(得分:0)

您可以使用基于比率的图像缩放来实现此目的。

此方法可以获得没有像素化的正确图像。

 float ori_height=image.size.height;
 float ori_width=image.size.width;



    NSString *orientation=@"";

    if (ori_height>ori_width)
    {
        orientation=@"PORTRAIT";
    }

    if (ori_width>ori_height)
    {
        orientation=@"LANDSCAPE";
    }


    if ([orientation isEqualToString:@"LANDSCAPE"])
    {





        if (ori_height<300)
        {

             int height_ratio=ori_height/4;
            int width_ratio=ori_width/4;

            UIGraphicsBeginImageContext(CGSizeMake(width_ratio,height_ratio));




           UIImage *currentImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(width_ratio, height_ratio)];


        }
        else
        {
            int height_ratio=ori_height/6;
            int width_ratio=ori_width/6;



            UIGraphicsBeginImageContext(CGSizeMake(width_ratio,height_ratio));


             UIImage *currentImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(width_ratio, height_ratio)];

        }

    }
    else if ([orientation isEqualToString:@"PORTRAIT"])
    {
        //NSLog(@"portrait");
        if (ori_height<300)
        {
           ...
        }
        else
        {
            int height_ratio=ori_height/6;
            int width_ratio=ori_width/6;



            UIGraphicsBeginImageContext(CGSizeMake(width_ratio,height_ratio));


         UIImage  *currentImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(width_ratio, height_ratio)];

            //[currentImage drawInRect: CGRectMake(0, 0, width_ratio, height_ratio)];

        }
    }



    UIGraphicsEndImageContext();

然后是另一种方法

-(UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{

UIGraphicsBeginImageContext(newSize); 

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();   

UIGraphicsEndImageContext();   

return newImage;
}