2013-03-10 97 views
-1

我有一个资源(.png文件)显示图片框架(边框)。
此.png文件大小为100x100px,边框宽度为10px。目标c - UIImage调整大小问题

my original image

我的问题:

如何从这个图像创建另一个UIImage的,具有不同的尺寸,不毁了边框的宽度是多少?

问题:

当我尝试从原始图像与CGContextDrawImage绘制新的图像我得到了新的尺寸新的形象,但我的边境比例废墟。

 CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newWidth, newHeight)); 
     CGImageRef imageRef = //... the image 

     // Build a context that's the same dimensions as the new size 
     CGContextRef bitmap = CGBitmapContextCreate(NULL, 
                newRect.size.width, 
                newRect.size.height, 
                CGImageGetBitsPerComponent(imageRef), 
                0, 
                CGImageGetColorSpace(imageRef), 
                CGImageGetBitmapInfo(imageRef)); 


     // Set the quality level to use when rescaling 
     CGContextSetInterpolationQuality(bitmap, kCGInterpolationHigh); 

     // Draw into the context; this scales the image 
     CGContextDrawImage(bitmap, newRect, imageRef); 

     // Get the resized image from the context and a UIImage 
     CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap); 
     UIImage *newImage = [UIImage imageWithCGImage:newImageRef]; 

     // Clean up 
     CGContextRelease(bitmap); 
     CGImageRelease(newImageRef); 

例如,当我试图创建一个图像尺寸800x100p,我得到的图像非常薄的顶部和底部边框。

the bad result I get

我需要的是,边界将保持不变,宽度
enter image description here

*注意
使用resizableImageWithCapInsets:不会帮助我,因为我需要一个新形象与新的大小保存在光盘上。

+1

是否必须是资源?我确定有一种方法可以通过编程方式添加宽度和颜色的边框。我认为这不是您的选择(如果您想要自定义边框等),只需确保。 – Sti 2013-03-10 16:29:02

+0

雅必须是.. – Eyal 2013-03-10 17:50:18

回答

1

您可以使用resizableImageWithCapInsets:

UIImage *img = [UIImage imageNamed:@"myResource"]; 
img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(10,10,10,10)]; 

我从来没有使用CGContextDrawImage这种方法,但它应该工作。

+0

我需要一个新的图像与我的目的新的大小.. – Eyal 2013-03-10 15:52:36

+0

resizableImageWithCapInsets:将创建一个resizableImage,它可以绘制任何大小。 – 2013-03-10 15:56:08

+0

我试过没有成功......你在哪里,如果我正在使用imageview来呈现图像,但我将图像写入光盘,并且它看起来像resizableImage对结果没有任何影响... – Eyal 2013-03-10 16:05:10