如何正确裁剪UIImage

时间:2014-07-14 01:13:08

标签: ios uiimage uiimagepickercontroller

我有一个具有横向方向的UIImage,我想在保持纵横比的同时切掉边缘使其成像。

  UIImage *photo = myUIImage;

  // width = 3264
  float width = photo.size.width;

  // height = 2448
  float height = photo.size.height;

我需要在切断左右边缘(保持中心)的同时得到以下尺寸:

  width = 1836
  height = 2448

2 个答案:

答案 0 :(得分:0)

idk如果有帮助,但这里是iPod,iPad,iPhone的正确尺寸

if ([[UIScreen mainScreen] bounds].size.height == 480)
{
    // iPhone, iPod Touch
    Image = [UIImage imageNamed:@"7.jpg"];
}

if ([[UIScreen mainScreen] bounds].size.height == 568)
{
    // iPhone 5
    Image = [UIImage imageNamed:@"7.jpg"];
}

if ([[UIScreen mainScreen] bounds].size.height == 1024)
{
    // iPad
    Image = [UIImage imageNamed:@"11@2x.jpg"];
}

self.view.backgroundColor = [UIColor colorWithPatternImage:Image];

答案 1 :(得分:0)

试试这个:

- (UIImage *)scaleOldImage:(UIImage *)orig toNewSize:(CGSize)finalSize {


CGSize origSize = orig.size;

CGFloat wScale = finalSize.width / origSize.width;
CGFloat hScale = finalSize.height / origSize.height;
CGFloat finalScale = (wScale > hScale) ? wScale : hScale;

CGRect finalRect = CGRectMake( 0, 0, origSize.width * finalScale, origSize.height * finalScale );
finalRect.origin.x = ( finalSize.width - finalRect.size.width )/2;
finalRect.origin.y = ( finalSize.height - finalRect.size.height )/2;

UIGraphicsBeginImageContextWithOptions( finalSize, YES, 0.0 );
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);

[orig drawInRect:finalRect];
UIImage * finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return finalImage;
}