【问题标题】:How to resize an image in iOS? [duplicate]如何在 iOS 中调整图像大小? [复制]
【发布时间】:2013-06-05 19:09:15
【问题描述】:

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

谢谢

【问题讨论】:

标签: ios uiimage


【解决方案1】:

尝试实现下面的代码。可能对你有帮助:

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

【讨论】:

  • 您好,感谢您的转发,如果我以 JPG 格式发送,如何更改 UIImagePNGRepresentation
  • 你可以使用 UIImageJPEGRepresentation ,它需要浮动的质量。
  • 为什么添加额外的步骤来创建PNG数据只是为了再次创建新图像?
  • 是的,你是对的,但我认为当时他要求我改变关于我记不太清的问题,但可能是因为我添加了它,否则图片1 很好。
  • 感谢@NitinGohel 的出色解决方案。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    试试这个代码:

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

    【讨论】:

      猜你喜欢
      • 2013-05-12
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      相关资源
      最近更新 更多