如何使用 - (UIImage *)stretchchableImageWithLeftCapWidth裁剪图像:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight

时间:2011-01-22 09:40:55

标签: iphone

我需要将单张图像裁剪成9张。

因为他们中的大多数都建议采用这种方法。

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

但是如果使用它的话,我也不知道。

任何人都可以帮助我。

如何使用此方法裁剪图像。

提前感谢你。

1 个答案:

答案 0 :(得分:3)

方法stretchableImageWithLeftCapWidth:topCapHeight未裁剪图像。它允许您拉伸垂直和水平边缘的图像。

在文档中,您将阅读:

  

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

希望这有帮助, 文森特