• After more than 15 years covering everything Apple, it’s with a heavy heart we announce that we will no longer be publishing new content on iMore and the iMore forums will be closing as of November 1st, 2024.

Can't getting pixels from UIImage

gairp

Member
May 12, 2011
6
0
0
Visit site
i want to get pixel color from UIImage, compare if is black color, and i have such funtion for duing this..
i need only my color at (GamePlayer.playerFrame.origin.x,GamePlayer.playerFrame.origin.y) pixel
and how can i get it?
Code:
-(BOOL) playerColidesWall
{
    BOOL result = NO;
    struct pixel* pixels = (struct pixel*) calloc(1, self.bounds.size.width * self.bounds.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap
        
        CGContextRef context = CGBitmapContextCreate(
                                                     (void*) pixels,
                                                     self.bounds.size.width,
                                                     self.bounds.size.height,
                                                     8,
                                                     self.bounds.size.width * 4,
                                                     CGImageGetColorSpace(currentLevelInfo.levelImage.CGImage),
                                                     kCGImageAlphaPremultipliedLast
                                                     );
        
        if (context != NULL)
        {            
            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height), currentLevelInfo.levelImage.CGImage);

            struct pixel black = {0,0,0,0};
            if(![self equalPixels:&pixels[(int)(GamePlayer.playerFrame.origin.x*GamePlayer.playerFrame.origin.y)]:&black])
           {
               result = YES;
           }
            CGContextRelease(context);
        }
        
        free(pixels);
    }
    return result;
}

this is equalPixels function
Code:
-(BOOL) equalPixels:(struct pixel*) pix1 :(struct pixel*) pix2
{
    NSLog(@"pixelcolor(%d,%d,%d)",pix1->r,pix1->b,pix1->g);
    return (pix1->r==pix2->r && pix1->b==pix2->b && pix1->g==pix2->g); 
}
//and this is pixles struct
struct pixel {
    unsigned char r, g, b, a;
};
 

gairp

Member
May 12, 2011
6
0
0
Visit site
Can't getting pixels from UIImage :(

i want to get pixel color from UIImage, compare if is black color, and i have such funtion for duing this..
i need only my color at (GamePlayer.playerFrame.origin.x,GamePlayer.playerFrame.origin.y) pixel
and how can i get it?
Code:
-(BOOL) playerColidesWall
{
    BOOL result = NO;
    struct pixel* pixels = (struct pixel*) calloc(1, self.bounds.size.width * self.bounds.size.height * sizeof(struct pixel));
    if (pixels != nil)
    {
        // Create a new bitmap
        
        CGContextRef context = CGBitmapContextCreate(
                                                     (void*) pixels,
                                                     self.bounds.size.width,
                                                     self.bounds.size.height,
                                                     8,
                                                     self.bounds.size.width * 4,
                                                     CGImageGetColorSpace(currentLevelInfo.levelImage.CGImage),
                                                     kCGImageAlphaPremultipliedLast
                                                     );
        
        if (context != NULL)
        {            
            CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, self.bounds.size.width, self.bounds.size.height), currentLevelInfo.levelImage.CGImage);

            struct pixel black = {0,0,0,0};
            if(![self equalPixels:&pixels[(int)(GamePlayer.playerFrame.origin.x*GamePlayer.playerFrame.origin.y)]:&black])
           {
               result = YES;
           }
            CGContextRelease(context);
        }
        
        free(pixels);
    }
    return result;
}

this is equalPixels function
Code:
-(BOOL) equalPixels:(struct pixel*) pix1 :(struct pixel*) pix2
{
    NSLog(@"pixelcolor(%d,%d,%d)",pix1->r,pix1->b,pix1->g);
    return (pix1->r==pix2->r && pix1->b==pix2->b && pix1->g==pix2->g); 
}
//and this is pixles struct
struct pixel {
    unsigned char r, g, b, a;
};
 

Trending Posts

Members online

Forum statistics

Threads
262,083
Messages
1,773,905
Members
441,389
Latest member
Jethro7