【问题标题】:ios: UIImage scale, UIImage and his appropriate CGimage are not identicalios:UIImage比例,UIImage和他对应的CGimage不一样
【发布时间】:2013-08-14 10:23:05
【问题描述】:

我有这段代码,其中self 是 UIImage 对象:

CGFloat scale = [sideSize floatValue] / MIN(self.size.width, self.size.height);
 UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.size.width*scale,self.size.height*scale), NO, 0.0);
[self drawInRect:CGRectMake(0, 0, self.size.width*scale, self.size.height*scale)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
SBLog(@"%f, %f", newImage.size.width, newImage.size.height);
return newImage;

但是当我使用函数UIImagePNGRepresentation 来获取字节数据以通过互联网传输时,我的图像字节在缩放后具有另一个大小。

当我使用[newImage CGImage] 时,在这段代码之后,我得到了相同的错误尺寸。

所以,我认为UIImagePNGRepresentation 使用 CGImage 从图像中获取数据字节。

那么,如何做到 UIImage 和 CGI​​mage 相同呢?

【问题讨论】:

  • “如何做相同的 UIImage 和 CGI​​mage”到底是什么意思?
  • 使尺寸相同。我想获取缩放图像的数据并传输它们,所以我不想传输更大的图像。我怎样才能得到以字节为单位的缩放图像。
  • 这意味着您为图像设置了一定的大小限制,如果图像大于此限制,您希望将图像缩小到其限制大小然后传输。对吗?
  • 是的,我想缩放图像并传输缩放图像。
  • 我已经决定这个任务来调整 CGImage 的大小,但是对于这个小任务来说,我认为它非常大

标签: ios ios5 uiimage scale cgimage


【解决方案1】:
//This method will resize the original image to desired width 
//maintaining the Aspect Ratio
-(UIImage*)getResizedToWidth:(CGFloat)width
{
    UIImage *resultImage = nil;

    CGFloat ar = self.size.width/self.size.height;
    CGFloat ht = width/ar;

    CGSize newSize = CGSizeMake(width, ht);

    UIGraphicsBeginImageContext(newSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -newSize.height);

    CGRect imageRect = CGRectMake(0, 0, newSize.width, newSize.height);

    CGContextDrawImage(ctx, imageRect, self.CGImage);

    resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resultImage;
}

//This method will resize the original image to desired height 
//maintaining the Aspect Ratio
-(UIImage*)getResizedToHeight:(CGFloat)height
{
    UIImage *resultImage = nil;

    CGFloat ar = self.size.width/self.size.height;
    CGFloat wd = height*ar;

    CGSize newSize = CGSizeMake(wd, height);

    UIGraphicsBeginImageContext(newSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -newSize.height);

    CGRect imageRect = CGRectMake(0, 0, newSize.width, newSize.height);

    CGContextDrawImage(ctx, imageRect, self.CGImage);

    resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resultImage;
}

//This method will take in the maxSizeLength and automatically
// detect the maximum side in image and reduce it to given
// maxSideLength maintaining the Aspect Ratio
-(UIImage*)getResizedMaxSideToLength:(CGFloat)maxSideLength
{
    UIImage *src = [UIImage imageWithCGImage:self.CGImage];

    if (src.size.width > maxSideLength)
    {
        src = [src getResizedToWidth:maxSideLength];
    }
    else
    if (src.size.height >= maxSideLength )
    {
        src = [src getResizedToHeight:maxSideLength];
    }

    return src;
}

当您需要 CGImage 时,只需访问任何 UIImage 的 CGImage 属性即可获取它,如下所示

CGImage *myCGImage = myUIImage.CGImage;

【讨论】:

  • 结果图片方向不同
  • 在这种情况下,您必须先修复图像的方向,然后才能使用我的代码执行调整大小操作。这是关于如何修复图像方向的链接stackoverflow.com/questions/5427656/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多