如何在iOS中调整图像大小?

iDi*_*Dia 25 uiimage ios

嗨,我想将图像发送到Web服务.但我想将用户选择的任何图像调整为75 x 75并将其发送到Web服务.如何将此图像调整为75 x 75

谢谢

Nit*_*hel 42

尝试实现以下代码.它可能对您有所帮助:

    CGRect rect = CGRectMake(0,0,75,75);
    UIGraphicsBeginImageContext( rect.size );
    [yourCurrentOriginalImage drawInRect:rect];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

  • 为什么还要添加额外的步骤,即仅创建PNG数据以再次创建新图像? (2认同)

man*_*jmv 33

使用此方法调整图像大小:

+(UIImage *)imageResize :(UIImage*)img andResizeTo:(CGSize)newSize
{
    CGFloat scale = [[UIScreen mainScreen]scale];
    /*You can remove the below comment if you dont want to scale the image in retina   device .Dont forget to comment UIGraphicsBeginImageContextWithOptions*/
    //UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, scale);
    [img drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
Run Code Online (Sandbox Code Playgroud)


kir*_*vda 8

试试这段代码:

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize
{
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();    
    UIGraphicsEndImageContext();
    return newImage;
}
Run Code Online (Sandbox Code Playgroud)