Proper use of UIRectClip to scale a UIImage down to icon size

前端 未结 3 495
清酒与你
清酒与你 2021-02-06 10:07

Given a UIImage of any dimension, I wish to generate a square \"icon\" sized version, px pixels to a side, without any distortion (stretching). How

相关标签:
3条回答
  • 2021-02-06 10:08

    I wanted to achieve a similar thing but found the answer from by the original poster didn't quite work. It distorted the image. This may well be solely because he didn't post the whole solution and had changed some of how the variables are initialised:

     (if (size.width > size.height) 
           ratio = px / size.width;
    

    Was wrong for my solution (which wanted to use the largest possible square from the source image). Also it is not necessary to use UIClipRect - if you make the context the size of the image you want to extract, no actual drawing will be done outside that rect anyway. It is just a matter of scaling the size of the image rect and offsetting one of the origin coordinates. I have posted my solution below:

    +(UIImage *)makeIconImage:(UIImage *)image
    {
        CGFloat destSize = 400.0;
        CGRect rect = CGRectMake(0, 0, destSize, destSize);
    
        UIGraphicsBeginImageContext(rect.size);
    
        if(image.size.width != image.size.height)
        {
            CGFloat ratio;
            CGRect destRect;
    
            if (image.size.width > image.size.height)
            {
                ratio = destSize / image.size.height;
    
                CGFloat destWidth = image.size.width * ratio;
                CGFloat destX = (destWidth - destSize) / 2.0;
    
                destRect = CGRectMake(-destX, 0, destWidth, destSize);
    
            }
            else
            {
                ratio = destSize / image.size.width;
    
                CGFloat destHeight = image.size.height * ratio;
                CGFloat destY = (destHeight - destSize) / 2.0;
    
                destRect = CGRectMake(0, destY, destSize, destHeight);
            }
            [image drawInRect:destRect];
        }
        else
        {
            [image drawInRect:rect];
        }
        UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return scaledImage;
    }
    
    0 讨论(0)
  • 2021-02-06 10:18

    Eureka! I had things a little mixed up. This works:

    CGRect clipRect = CGRectMake(-offset.x, -offset.y,
                                 (ratio * size.width) + delta,
                                 (ratio * size.height) + delta);
    UIGraphicsBeginImageContext(CGSizeMake(px, px));
    UIRectClip(clipRect);
    [myImage drawInRect:clipRect];
    UIImage *icon = UIGraphicsGetImageFromCurrentImageContext();
    
    // Do something with the icon here ...
    
    UIGraphicsEndImageContext();
    

    No more need for rect. The trick appears to be using a negative offset in the clipping rectangle, thereby lining up the origin of where we want to grab our 50 x 50 image (in this example).

    Perhaps there's an easier way. If so, please weigh in!

    0 讨论(0)
  • 2021-02-06 10:26

    wheeliebin answers is correct but he forgot a minus sign in front of destY

    destRect = CGRectMake(0, -destY, destSize, destHeight);
    
    0 讨论(0)
提交回复
热议问题