2013-06-19 124 views
0

我畫了一些手指觸摸使用位圖。繪製位圖到屏幕模糊?

首先我畫線到位圖。

然後將位圖繪製到屏幕上。

但問題是使用這種方式,線條模糊。

下面是一個比較。

enter image description here

enter image description here

圖片1被直接繪製到屏幕上。 picture2正在使用位圖。 這是我的代碼:

- (BOOL)CreateBitmapContext:(int)pixelsWide High:(int)pixelsHight 
{ 
CGColorSpaceRef colorSpace; 
void *   bitmapData; 
int   bitmapByteCount; 
int   bitMapBytesPerRow; 

bitMapBytesPerRow = (pixelsWide * 4); 
bitmapByteCount = (bitMapBytesPerRow * pixelsHight); 

colorSpace = CGColorSpaceCreateDeviceRGB(); 
bitmapData = malloc(bitmapByteCount); 
if (bitmapData == NULL) 
{ 
    CGColorSpaceRelease(colorSpace); 
    return NO; 
} 
tempContext = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHight, 8, bitMapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast); 



if (tempContext == NULL) 
{ 
    CGColorSpaceRelease(colorSpace); 
    free(bitmapData); 
    return NO; 
} 
CGColorSpaceRelease(colorSpace); 
return YES; 
} 


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint p = [touch locationInView:self]; 
[tool setFirstPoint:p]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint p = [touch locationInView:self]; 
CGPoint previous = [touch previousLocationInView:self]; 
[tool moveFromPoint:previous toPoint:p]; 
[tool draw:tempContext]; 
[self setNeedsDisplay]; 
} 


- (void)drawRect:(CGRect)rect 
{ 
// Drawing code 
CGContextRef myContext = UIGraphicsGetCurrentContext(); 

myImage = CGBitmapContextCreateImage (tempContext);// 5 
CGContextDrawImage(myContext, rect, myImage);// 6 
} 

那麼它有什麼問題?

有人嗎?

+1

對於視網膜設備上下文,您應該使用雙倍高度和寬度。 – yinkou

回答

1

您的設備可能有一個視網膜屏幕。在這些屏幕上,您需要分配一個與屏幕像素分辨率相同的位圖,否則當您將其渲染回來時會有點模糊。令人困惑的部分是,如果您只需要[[UIScreen mainScreen] bounds]來確定像素分辨率,那麼您的圖像將是所需像素分辨率的一半。這是因爲邊界值代表密度無關的點,而不是像素。您也可以通過閱讀[[UIScreen mainscreen] scale]來彌補這一點,這將在視網膜設備上返回2.0

+0

是的,你應該乘以' - (BOOL)CreateBitmapContext:(int)pixelsWide High:(int)pixelsHight'通過'[[UIScreen mainScreen] scale]' – Idles

+0

傳入的值,但是當我輸入如下代碼: 'float scaleFactor = [[UIScreen mainScreen] scale]; myBitmapContext = CGBitmapContextCreate(bitmapData,size.width,size.height,8,bitmapBytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast); 如果(視網膜) { CGContextScaleCTM(myBitmapContext,比例因子,比例因子); }' 它仍然顯示不正確 – Domlin

+0

不要設置上下文比例,更改您創建的上下文的大小。也就是說,無條件地乘以CGBitmapContextCreate中的scaleFactor(在非視網膜屏幕上的比例是1.0,所以你不需要if-check)。 – Idles