代码之家  ›  专栏  ›  技术社区  ›  choise

调整图像大小并将其居中裁剪

  •  16
  • choise  · 技术社区  · 14 年前

    因此,目前我正在尝试裁剪和调整图片大小,以使其适合特定的大小,而不会丢失比例。

    显示我意思的小图片:

    我在 vocaro's categories 上玩了一点,但是它们不适用于png's,并且在gifs上有问题。另外,图像不会被剪切。

    有人有没有建议过如何以最好的方式调整大小,或者有没有链接到现有的库/分类/任何东西?

    谢谢你的提示!

    另外:iOS是否实现了一个“选择一个摘录”,这样我就有了正确的比率,并且只需要缩放它?!在不损失比率的情况下将其转换成特定的大小。

    显示我意思的小图片:

    alt text

    我玩过一点 vocaro's categories 但他们不与巴布亚新几内亚的工作,并与gif有问题。而且图像不会被剪切。

    有人有没有建议过如何以最好的方式调整大小,或者有没有链接到现有的库/分类/任何东西?

    谢谢你的提示!

    另外:iOS是否实现了一个“选择一个摘录”,这样我就有了正确的比率,并且只需要缩放它?!

    2 回复  |  直到 8 年前
        1
  •  7
  •   par    8 年前

    这个方法将做你想要的,并且是一个易用的uiimage类别。我先调整大小然后裁剪,如果你想裁剪然后再调整大小,你可以很容易地切换代码。函数中的边界检查纯粹是说明性的。您可能需要做一些不同的事情,例如,将裁剪矩形相对于输出图像尺寸居中,但这应该使您足够接近,以便进行任何其他需要的更改。

    @implementation UIImage( resizeAndCropExample )
    
    - (UIImage *) resizeToSize:(CGSize) newSize thenCropWithRect:(CGRect) cropRect {
        CGContextRef                context;
        CGImageRef                  imageRef;
        CGSize                      inputSize;
        UIImage                     *outputImage = nil;
        CGFloat                     scaleFactor, width;
    
        // resize, maintaining aspect ratio:
    
        inputSize = self.size;
        scaleFactor = newSize.height / inputSize.height;
        width = roundf( inputSize.width * scaleFactor );
    
        if ( width > newSize.width ) {
            scaleFactor = newSize.width / inputSize.width;
            newSize.height = roundf( inputSize.height * scaleFactor );
        } else {
            newSize.width = width;
        }
    
        UIGraphicsBeginImageContext( newSize );
    
        context = UIGraphicsGetCurrentContext();
    
        // added 2016.07.29, flip image vertically before drawing:
        CGContextSaveGState(context);
        CGContextTranslateCTM(context, 0, newSize.height);
        CGContextScaleCTM(context, 1, -1);
        CGContextDrawImage(context, CGRectMake(0, 0, newSize.width, newSize.height, self.CGImage);
    
    //  // alternate way to draw
    //  [self drawInRect: CGRectMake( 0, 0, newSize.width, newSize.height )];
    
        CGContextRestoreGState(context);
    
        outputImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        inputSize = newSize;
    
        // constrain crop rect to legitimate bounds
        if ( cropRect.origin.x >= inputSize.width || cropRect.origin.y >= inputSize.height ) return outputImage;
        if ( cropRect.origin.x + cropRect.size.width >= inputSize.width ) cropRect.size.width = inputSize.width - cropRect.origin.x;
        if ( cropRect.origin.y + cropRect.size.height >= inputSize.height ) cropRect.size.height = inputSize.height - cropRect.origin.y;
    
        // crop
        if ( ( imageRef = CGImageCreateWithImageInRect( outputImage.CGImage, cropRect ) ) ) {
            outputImage = [[[UIImage alloc] initWithCGImage: imageRef] autorelease];
            CGImageRelease( imageRef );
        }
    
        return outputImage;
    }
    
    @end
    
        2
  •  2
  •   shashwat    10 年前

    我在一个应用程序中遇到了同样的问题,并开发了这段代码:

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

    您可以根据需要修改它。