2011-10-02 50 views
1

colorWithPatternImage在iphone創建黑色網格4

UIView *topPart = [[UIView alloc] initWithFrame:CGRectMake(9, 0, 302, 318)]; 
topPart.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pattern.png"]]; 
[someScroller addSubview:topPart]; 
[topPart release]; 

,我可以罰款看到的圖案,當我用一個3GS或iPhone模擬器,但是當我在實際的iPhone 4測試,我看到這個模式圖像,但有黑色的線條分隔每個模式,因此它看起來像一個網格。我檢查了圖案圖像,沒有黑色邊框或任何東西。

有一個人用不同的問題,但它可能是相同的解決方案 colorWithPatternImage with iPhone 4 Retina Display ([email protected])

,但他們建議使用 - (空)drawRect:方法,唯一的問題是我不知道該怎麼做。

感謝

+0

的可能重複:http://stackoverflow.com/questions/4886019/2x-images-for-iphone-os-4-0 – Raj

回答

1

我想通了很多答案來自這兩個職位合併 https://devforums.apple.com/message/259150#259150

How can I use CGContextDrawTiledImage to tile an image?

UIImage *thePattern= [self GetFineTiles:[UIImage imageNamed:@"Feedbackpattern.png"]]; 
theView.backgroundColor = [UIColor colorWithPatternImage:thePattern]; 

-(UIImage*)GetFineTiles:(UIImage*)badImage{ 
    UIGraphicsBeginImageContext(badImage.size); 
    CGContextRef imageContext = UIGraphicsGetCurrentContext(); 
    CGContextDrawTiledImage(imageContext, (CGRect){CGPointZero,badImage.size}, badImage.CGImage); 
    UIImage *goodPattern = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return goodPattern; 
} 

希望這有助於他人

2

我遇到同樣的問題,發現這是由於我的PNG是灰度(而不是RGB)

要檢查是否屬於這種情況,只需在Xcode中選擇圖像,顯示Utilities邊欄(從右邊出現的邊欄)並查看Color Space。

爲了解決這個問題,你可以使用自己喜歡的圖像編輯器,但爲Photoshop這樣做: 打開灰度圖像 全選,複製 創建一個新的文件(但此時要確保色彩空間是RGB) 粘貼

希望它能幫助:)