2016-08-25 58 views
0

我試圖用平鋪的圖案創建一個圖像,但平鋪的圖像似乎有一個1像素的透明邊框。即使當我僅使用白色背景圖像時,我仍然可以看到透明邊框。colworWithPatternImage返回一個1像素的透明邊框

UIImage *patternImage=[UIImage imageNamed:@"whiteimage.png"]; 
CALayer *layer=[[CALayer alloc] init]; 
layer.frame=CGRectMake(0, 0, 2048, 1536); 
layer.backgroundColor=[UIColor colorWithPatternImage:patternImage].CGColor; 
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0,0,2048,1536)]; 
[view setOpaque:YES]; 
[view.layer addSublayer:layer]; 

格式圖像 pattern image

平鋪圖像與透明像素邊框 tiled image

圖案圖像是使用雙三次重採樣從Photoshop中導出的,但我也試過雙線性和最近鄰方法。另外,如果我在Photoshop中使用圖像作爲圖案,我不會得到透明邊框,這導致我認爲它與iOS相關。

+0

這實際上是您使用的圖案圖像嗎?它怎麼這麼大? –

+0

白色圖案圖像不一定非常大,但我使用的樣本具有相似的大小。平鋪的圖像用於形成一個應用了透視變換的地板,因此需要非常大才能填滿地面。 – WagglyWonga

+0

上面的代碼對我來說工作正常,你還對圖像和視圖做了什麼?例如在第二張圖片中,平鋪圖像是否小得多? –

回答

0
-(UIImage*) resizeImage:(UIImage*)image newSize:(CGSize)newSize 
{ 
     UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
     [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; 
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext();  
     return newImage; 
} 

我忘了在我的問題中包含我的幫助方法,該方法縮放了平鋪圖像。正是這種方法給了我這個問題。當我通過像素增加寬度和高度時,我不再在返回的圖像中獲得透明邊框。

-(UIImage*) resizeImage:(UIImage*)image newSize:(CGSize)newSize 
{ 
     UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); 
     [image drawInRect:CGRectMake(0, 0, newSize.width+1, newSize.height+1)]; 
     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext();  
     return newImage; 

}