Cropping an UIImage

后端 未结 24 1798
轮回少年
轮回少年 2020-11-22 02:45

I\'ve got some code that resizes an image so I can get a scaled chunk of the center of the image - I use this to take a UIImage and return a small, square repre

24条回答
  •  北海茫月
    2020-11-22 03:45

    I use the method below.

      -(UIImage *)getNeedImageFrom:(UIImage*)image cropRect:(CGRect)rect
      {
        CGSize cropSize = rect.size;
        CGFloat widthScale =  
        image.size.width/self.imageViewOriginal.bounds.size.width;
        CGFloat heightScale = 
        image.size.height/self.imageViewOriginal.bounds.size.height;
        cropSize = CGSizeMake(rect.size.width*widthScale,  
        rect.size.height*heightScale);
        CGPoint  pointCrop = CGPointMake(rect.origin.x*widthScale, 
        rect.origin.y*heightScale);
        rect = CGRectMake(pointCrop.x, pointCrop.y, cropSize.width, 
        cropSize.height);
        CGImageRef subImage = CGImageCreateWithImageInRect(image.CGImage, rect);
        UIImage *croppedImage = [UIImage imageWithCGImage:subImage];
        CGImageRelease(subImage);
        return croppedImage;
    

    }

提交回复
热议问题