2013-03-10 101 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