resize and crop image centered

前端 未结 3 551
半阙折子戏
半阙折子戏 2021-01-31 11:11

so currently i\'m trying to crop and resize a picture to fit it into a specific size without losing the ratio.

a small image to show what i mean:

3条回答
  •  抹茶落季
    2021-01-31 11:43

    I have came across the same issue in one of my application and developed this piece of code:

    + (UIImage*)resizeImage:(UIImage*)image toFitInSize:(CGSize)toSize
    {
        UIImage *result = image;
        CGSize sourceSize = image.size;
        CGSize targetSize = toSize;
    
        BOOL needsRedraw = NO;
    
        // Check if width of source image is greater than width of target image
        // Calculate the percentage of change in width required and update it in toSize accordingly.
    
        if (sourceSize.width > toSize.width) {
    
            CGFloat ratioChange = (sourceSize.width - toSize.width) * 100 / sourceSize.width;
    
            toSize.height = sourceSize.height - (sourceSize.height * ratioChange / 100);
    
            needsRedraw = YES;
        }
    
        // Now we need to make sure that if we chnage the height of image in same proportion
        // Calculate the percentage of change in width required and update it in target size variable.
        // Also we need to again change the height of the target image in the same proportion which we
        /// have calculated for the change.
    
        if (toSize.height < targetSize.height) {
    
            CGFloat ratioChange = (targetSize.height - toSize.height) * 100 / targetSize.height;
    
            toSize.height = targetSize.height;
            toSize.width = toSize.width + (toSize.width * ratioChange / 100);
    
            needsRedraw = YES;
        }
    
        // To redraw the image
    
        if (needsRedraw) {
            UIGraphicsBeginImageContext(toSize);
            [image drawInRect:CGRectMake(0.0, 0.0, toSize.width, toSize.height)];
            result = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
    
        // Return the result
    
        return result;
    }
    

    You can modify it according to your needs.

提交回复
热议问题