Domlin
Domlin

Reputation: 195

Drawing bitmap to screen are blurred???

I draw something finger touch use bitmap.

First I draw the line to bitmap.

Then draw bitmap to the screen.

But the question is using this way,lines are blurred.

Below is a comparison.

enter image description here

enter image description here

picture1 is directly drawing onto the screen. picture2 is using bitmap. Here is my code:

 - (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  
}

So what wrong with it??

Anyone?

Upvotes: 0

Views: 336

Answers (1)

Idles
Idles

Reputation: 1131

Your device probably has a retina screen. On those screens, you need to allocate a bitmap that is the same pixel resolution of the screen, or it'll be a little blurry when you render it back. The confusing part is if you just do [[UIScreen mainScreen] bounds] to determine the pixel resolution, then your image will be half the needed pixel resolution. This is because the bounds values represent density-independent points, not pixels. You compensate for this by also reading [[UIScreen mainscreen] scale] which will return 2.0 on retina devices.

Upvotes: 1

Related Questions