2013-06-19 76 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