2012-07-12 35 views
21

我想在我的IOS應用程序中使用9補丁圖像。可能嗎?如何在IOS中使用9-patch圖像?

+4

Google ban :)? 1)SO http://stackoverflow.com/questions/5570818/android-nine-patch-equivalent-for-other-platforms 2)Tortuga http://maniacdev.com/2011/07/open-source-library-and -guide-for-ninepatch-image-support-on-ios-platforms/ – 2012-07-12 08:01:30

回答

17
+13

這就是不是9補丁。 – 2012-07-12 08:28:27

+0

你是對的resizableImageWithCapInsets:很好。但它不是9個補丁。它將允許您應用有關如何調整圖像大小的一些規則。但它不會複製9補丁。 – bturner 2013-03-05 16:41:30

+0

是的,上面的答案是正確的,但9補丁是比它更好的選擇 – 2016-10-07 04:51:12

2

你可以試試這個簡單的實現: https://github.com/shiami/SWNinePatchImageFactory

的概念是很簡單的描述如下:

該技術只是將9修補程序PNG圖像轉換爲iOS兼容,可調整大小的UIImage對象。 請參閱UIImage的方法resizableImageWithCapInsets:(UIEdgeInsets)insets以獲取更多信息。 所以它只支持在水平和垂直兩側伸展一段補丁標記。

+ (UIImage*)createResizableImageFromNinePatchImage:(UIImage*)ninePatchImage 
{ 
    NSArray* rgbaImage = [self getRGBAsFromImage:ninePatchImage atX:0 andY:0 count:ninePatchImage.size.width * ninePatchImage.size.height]; 
    NSArray* topBarRgba = [rgbaImage subarrayWithRange:NSMakeRange(1, ninePatchImage.size.width - 2)]; 
    NSMutableArray* leftBarRgba = [NSMutableArray arrayWithCapacity:0]; 
    int count = [rgbaImage count]; 
    for (int i = 0; i < count; i += ninePatchImage.size.width) { 
     [leftBarRgba addObject:rgbaImage[i]]; 
    } 

    int top = -1, left = -1, bottom = -1, right = -1; 
    count = [topBarRgba count]; 
    for (int i = 0; i <= count - 1; i++) { 
     NSArray* aColor = topBarRgba[i]; 
     if ([aColor[3] floatValue] == 1) { 
      left = i; 
      break; 
     } 
    } 
    NSAssert(left != -1, @"The 9-patch PNG format is not correct."); 
    for (int i = count - 1; i >= 0; i--) { 
     NSArray* aColor = topBarRgba[i]; 
     if ([aColor[3] floatValue] == 1) { 
      right = i; 
      break; 
     } 
    } 
    NSAssert(right != -1, @"The 9-patch PNG format is not correct."); 
    for (int i = left + 1; i <= right - 1; i++) { 
     NSArray* aColor = topBarRgba[i]; 
     if ([aColor[3] floatValue] < 1) { 
      NSAssert(NO, @"The 9-patch PNG format is not support."); 
     } 
    } 
    count = [leftBarRgba count]; 
    for (int i = 0; i <= count - 1; i++) { 
     NSArray* aColor = leftBarRgba[i]; 
     if ([aColor[3] floatValue] == 1) { 
      top = i; 
      break; 
     } 
    } 
    NSAssert(top != -1, @"The 9-patch PNG format is not correct."); 
    for (int i = count - 1; i >= 0; i--) { 
     NSArray* aColor = leftBarRgba[i]; 
     if ([aColor[3] floatValue] == 1) { 
      bottom = i; 
      break; 
     } 
    } 
    NSAssert(bottom != -1, @"The 9-patch PNG format is not correct."); 
    for (int i = top + 1; i <= bottom - 1; i++) { 
     NSArray* aColor = leftBarRgba[i]; 
     if ([aColor[3] floatValue] == 0) { 
      NSAssert(NO, @"The 9-patch PNG format is not support."); 
     } 
    } 

    UIImage* cropImage = [ninePatchImage crop:CGRectMake(1, 1, ninePatchImage.size.width - 2, ninePatchImage.size.height - 2)]; 

    return [cropImage resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)]; 
} 

SWNinePatchImageView還提供了Nib或Storyboard的用法。 您可以檢查回購中的示例項目。