How to crop a UIImageView to a new UIImage in 'aspect fit' mode?

后端 未结 4 1119
一生所求
一生所求 2020-12-28 10:51

\"enter

I would like to create a new UIImage by cropping the image inside a UIImageVie

4条回答
  •  暖寄归人
    2020-12-28 10:54

    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;
    
       }
    

提交回复
热议问题