2011-01-22 38 views
1

我需要裁剪單個圖像爲9個部分。如何裁剪圖像使用 - (UIImage *)stretchableImageWithLeftCapWidth :(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

對於大多數建議休耕方法。

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight 

,但我沒有任何地方是怎麼做到的使用這個罰款。

任何一個可以請幫助我。

我怎麼可以用這種方法裁剪圖像。

謝謝你提前。

+0

老兄......你已經問過這個問題的http://計算器.COM /問題/ 4743242 /如何對作物圖像輸入到塊,編程/ 4744599#4744599(順便說一下,我在你前面的問題給出的答案是正確的) – fsaint 2011-01-22 09:52:21

回答

3

方法stretchableImageWithLeftCapWidth:topCapHeight不裁剪圖像。它允許您用垂直和水平的邊緣拉伸圖像。

在DOC你會讀到:

stretchableImageWithLeftCapWidth:topCapHeight: 創建並返回一個新的圖像對象 與指定的上限數值。

要裁剪圖像,你應該使用CGImageCreateWithImageInRect:

CGImageCreateWithImageInRect 創建使用包含現有的位圖圖像的分區中的數據的位圖圖像。

你可以實現你想要的一個方法,像這樣的:

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect { 

    CGImageRef sourceImageRef = [image CGImage]; 
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect); 
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef scale:1.0 orientation:image.imageOrientation]; 
    CGImageRelease(newImageRef); 
    return newImage; 
} 

希望這有助於 文森特