JAHelia
JAHelia

Reputation: 7902

Animating backgroundColor of a UIView when using colorWithPatternImage

I'm using the following code snippet when I animate the backgroundColor property of a UIView:

  [UIView beginAnimations:@"fade" context:nil];
     [UIView setAnimationDuration:1];
    [[self view] setBackgroundColor: [UIColor greenColor];
    [UIView commitAnimations];

this works well with basic colors (red, green, gray, etc ...), however, when I try to use a color like this: [UIColor colorWithPatternImage:[UIImage imageNamed:@"img1.png"]]; the animation will not work, the view will set its new background but without the desired fade animation.

How can I animate my UIView's backgroundColor to accept colorWithPatternImage ?

thank you so much in advance.

Upvotes: 4

Views: 1827

Answers (2)

Nitin
Nitin

Reputation: 7461

I have changed my code..use this and it's work 100%...i tested it..

   //Function which will give you RGB color from your image..(copy and add this function in your code before calling it)
- (UIColor*)getRGBAsFromImage: (UIImage*)image atX: (int)xx andY: (int)yy count: (int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];

// First get the image into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
                                             bitsPerComponent, bytesPerRow, colorSpace,
                                             kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);

// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
UIColor *acolor;
for (int ii = 0 ; ii < count ; ++ii)
{
    CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
    CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
    CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
    CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
    byteIndex += 4;

    acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
    [result addObject:acolor];
}

free(rawData);
return acolor;
}


// Here, the code for animation that you want..use it and enjoy..
UIImage *image = [UIImage imageNamed:@"Black.png"];
    UIColor *color=  [self getRGBAsFromImage:image atX:100 andY:100 count:1];
    [UIView beginAnimations:@"fade" context:nil];
    [UIView setAnimationDuration:1];
    [[self view] setBackgroundColor: color];
    [UIView commitAnimations];

It's worked fine...hope this will help you...

Upvotes: 1

meronix
meronix

Reputation: 6176

i guess that all the colors used in the steps needed for the animations are colors with component RGB values between the starting RGB color and the ending RGB color...

but of course thats impossible if you use an image...

so try a workaround, as with a second temp uiview that fade-in with an alpha = 0 to alpha = 1 value-changing...

Upvotes: 2

Related Questions