Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-13-2009, 03:01 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 15
Default Drawing Pixels in a BitmapContext is very slow. Need Help.

Hello!

I'm pretty new to the iPhone developing subject.
My idea is to draw pixels on the screen. Actually I got it working but it is very slow. In the simulator it is ok, but on the iPhone I got around three frames per second or so.

Right now I'm just changing three bytes in the buffer from my BitmapContext every time the drawRect method runs. But I guess it is drawing a complete new image.

Do you guys have some ideas on how to optimize this code?
Is there a way of avoiding creating a complete new image and drawing it into the graphics context?

PHP Code:
//
//  DrawingView.m
//

#import "DrawingView.h"


@implementation DrawingView


@synthesize firstTouch;
@
synthesize lastTouch;

- (
id)initWithCoder:(NSCoder*)coder
{
     if ( ( 
self = [super initWithCoder:coder] ) )
     {
          
firstRun TRUE;
          [
self setupBitmapContext];
     }
     
     return 
self;
}


- (
void)drawRect:(CGRect)rect
{
     
CGContextRef myContext UIGraphicsGetCurrentContext();

     if (
bitmapData != NULL)
     {
          
int posY MAX(0roundpixelsHigh lastTouch.1.0));
          
int posX roundlastTouch.);
          
int offset * ( ( pixelsWide posY ) + posX );
          
bitmapData[offset+1] = 255;
          
bitmapData[offset+2] = 255;
          
bitmapData[offset+3] = 255;
     }
     
     
bitmapImage CGBitmapContextCreateImage (bitmapContext);
     
CGContextDrawImage(myContextbitmapRectbitmapImage);
}


-(
void)setupBitmapContext
{
     
pixelsWide 320;
     
pixelsHigh 400;
     
bitmapBytesPerRow   = (pixelsWide 4);
     
bitmapByteCount     = (pixelsWide pixelsHigh 4);
     
colorSpace CGColorSpaceCreateDeviceRGB();
     if (
colorSpace == NULL)     NSLog(@"Error allocating color space");
     
     
bitmapData mallocbitmapByteCount );
     if (
bitmapData == NULL)
     {
          
NSLog(@"Memory not allocated!");
          
CGColorSpaceReleasecolorSpace );
     }
     
     
bitmapContext CGBitmapContextCreate (bitmapData,
                                         
pixelsWide,
                                                  
pixelsHigh,
                                                  
8,
                                                  
bitmapBytesPerRow,
                                                  
colorSpace,
                                                  
kCGImageAlphaPremultipliedFirst);
     if (
bitmapContext == NULL)
     {
          
free (bitmapData);
          
NSLog(@"Context not created!");
     }
     
     
bitmapRect CGRectMake(0.00.0pixelsWidepixelsHigh-3);
}


- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     
UITouch *touch = [touches anyObject];
     
firstTouch = [touch locationInView:self];
     
lastTouch = [touch locationInView:self];
     [
self setNeedsDisplay];
}


- (
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
     
UITouch *touch = [touches anyObject];
     
lastTouch = [touch locationInView:self];

     [
self setNeedsDisplay];
}


- (
void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
     
UITouch *touch = [touches anyObject];
     
lastTouch = [touch locationInView:self];

     [
self setNeedsDisplay];
}




- (
void)dealloc
{
     [
super dealloc];
}

@
end 
You can also download this class here: http://www.rwichmann.com/downloads/DrawingView.m


Thank you for every tipp!

Cheers,
Raphael
Raphael is offline   Reply With Quote
Reply

Bookmarks

Tags
bitmapcontext, context, iphone, pixel, setpixel

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 235
11 members and 224 guests
ADY, CKAmike, Dani77, Duncan C, HemiMG, Promo Dispenser, Punkjumper, Rudy, sacha1996, sneaky, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:21 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0