简体   繁体   English

如何在iPhone中裁剪UIImage?

[英]How to crop the UIImage in iPhone?

在我的应用程序中,我在UIImageView中设置了一张图像,UIImageView的大小为320 x170。但是原始图像的大小为320 x460。因此如何裁剪此图像并在UIImageView中显示。

Here is a good way to crop an image to a CGRect: 这是将图像裁剪为CGRect的好方法:

- (UIImage*)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect

{

   //create a context to do our clipping in

   UIGraphicsBeginImageContext(rect.size);

   CGContextRef currentContext = UIGraphicsGetCurrentContext();

   //create a rect with the size we want to crop the image to
   //the X and Y here are zero so we start at the beginning of our
   //newly created context

   CGRect clippedRect = CGRectMake(0, 0, rect.size.width, rect.size.height);

   CGContextClipToRect( currentContext, clippedRect);

   //create a rect equivalent to the full size of the image
   //offset the rect by the X and Y we want to start the crop
   //from in order to cut off anything before them

   CGRect drawRect = CGRectMake(rect.origin.x * -1,
                                rect.origin.y * -1,
                                imageToCrop.size.width,
                                imageToCrop.size.height);

   //draw the image to our clipped context using our offset rect

   CGContextDrawImage(currentContext, drawRect, imageToCrop.CGImage);

   //pull the image from our cropped context

   UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();

   //pop the context to get back to the default

   UIGraphicsEndImageContext();

   //Note: this is autoreleased

   return cropped;

}

Or another way: 或另一种方式:

- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
     

{
  CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
    

  UIImage *cropped = [UIImage imageWithCGImage:imageRef];

  CGImageRelease(imageRef);


      return cropped;
    

}

From http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/ . 来自http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/

You can call this function for cropping the image - 您可以调用此函数来裁剪图像-

- (UIImage *)resizeImage:(UIImage *)oldImage width:(float)imageWidth height:(float)imageHeight {
    UIImage *newImage = oldImage;

    CGSize itemSize = CGSizeMake(imageWidth, imageHeight);
    UIGraphicsBeginImageContext(itemSize);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [oldImage drawInRect:imageRect];

    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

this function returns the UIImage . 此函数返回UIImage

Maybe someone is interested in the Swift version of the answer @tirth gave. 也许有人对@tirth给出的答案的Swift版本感兴趣。 It is written as a UIImage extension. 它被编写为UIImage扩展。 As an example I added another method to crop the image to be a center square version. 作为示例,我添加了另一种方法来将图像裁剪为中心正方形版本。

    // MARK: - UIImage extension providing function to crop an image to a rect
    extension UIImage {
        /**
         Return a cropped image from an existing image

         - parameter toRect: a rectangular region for a new image

         - returns: new image instance
         */
        func croppedImage(toRect: CGRect) -> UIImage {
            // create new CGImage reference
            let imageRef = CGImageCreateWithImageInRect(self.CGImage, toRect)
            // create and return new UIImage
            return UIImage(CGImage: imageRef!)
        }

        /**
         Crop center rect from possibly rectangular image

         - returns: return self in case image is already square, new center rect otherwise
         */
        func cropCenterRect() -> UIImage {
            // image might already be square
            if self.size.height == self.size.width {
                return self
            }
            // portrait
            if self.size.height > self.size.width {
                // calculate offset at top and bottom
                let offset = (self.size.height - self.size.width) / 2.0
                // return cropped image
                return self.croppedImage(CGRect(x: 0.0, y: offset, width: self.size.width, height: self.size.width))
            } else {
                // landscape
                // calculate offset left and right
                let offset = (self.size.width - self.size.height) / 2.0
                // return cropped image
                return self.croppedImage(CGRect(x: offset, y: 0.0, width: self.size.height, height: self.size.height))
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM