我想在我的IOS應用程序中使用9補丁圖像。可能嗎?如何在IOS中使用9-patch圖像?
回答
調查UIImage的方法resizableImageWithCapInsets:(UIEdgeInsets)capInsets
。
這就是不是9補丁。 – 2012-07-12 08:28:27
你是對的resizableImageWithCapInsets:很好。但它不是9個補丁。它將允許您應用有關如何調整圖像大小的一些規則。但它不會複製9補丁。 – bturner 2013-03-05 16:41:30
是的,上面的答案是正確的,但9補丁是比它更好的選擇 – 2016-10-07 04:51:12
最有可能的結果是 https://github.com/andylanddev/Tortuga22-NinePatch 9補丁庫已經使用波紋管方法 resizableImageWithCapInsets:(UIEdgeInsets)鑲石
你可以試試這個簡單的實現: 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的用法。 您可以檢查回購中的示例項目。
- 1. 如何使用9patch圖像製作ImageButton?
- 2. 無法創建9patch圖像
- 3. 在Xamarin iOS中使用圖像視圖
- 4. Android:如何使用9patch創作者?
- 5. aapt無法找到9Patch圖像
- 6. 麻煩縮放9patch圖像均勻
- 7. 只有水平縮放的9patch圖像
- 8. 進度條9patch圖像水平刻度
- 9. 9patch圖像看起來很模糊
- 10. 強制9patch圖像以適合寬度,使用填充
- 11. 如何修改9patch圖像Android的顏色?
- 12. 如何使用默認IOS圖像
- 13. 如何在我的iOS應用程序中使用svg圖像
- 14. 在Xamarin IOS中使用Vector pdf圖像
- 15. 如何在iOS中使用籃子圖像捕捉動畫蘋果圖像?
- 16. 如何在iOS中使用多個圖像選擇器與swift
- 17. 如何使用最新的iOS SDK在Pinterest中共享圖像
- 18. 如何使用Cloudinary在iOS中執行圖像轉換操作
- 19. 如何在iOS 8中使用swift插入圖像Inline UILabel
- 20. 如何在UIImageView中使用gif圖像iOS
- 21. 如何在iOS中高效地使用圖像動畫
- 22. 如何在IOS中使用橫向圖像填充背景?
- 23. 如何在UIImageView iOS中使用啓動圖像集?
- 24. 如何使用CoreText在富文本中繪製圖像? (iOS)
- 25. 如何在iOS中使用POST請求發送圖像
- 26. 如何在iOS中複製圖像?
- 27. 如何在iOS中創建圖像庫
- 28. 如何在IOS sdk中掩蓋圖像?
- 29. 如何在ios中預加載圖像
- 30. 如何在iOS中壓縮圖像?
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