Oscar
Oscar

Reputation: 1929

UIImageView setImage doesn't work

I'm making an animation calling repeatedly this function so I can make an animation:

The method where I have the issue, is a method of a UIImageView's subclass Class

 @interface MyCustomImageView : UIImageView {

& it is the following:

- (void) animationShowFrame: (NSInteger) frame {
    NSLog(@"frame: %d", frame);

    if ((frame >= animationNumFrames) || (frame < 0))
        return;

    NSData *data = [animationData objectAtIndex:frame];
    UIImage *img = [UIImage imageWithData:data];
    NSLog(@"%@",img);
    NSLog(@"data: %d", [data length]);
    [self setImage:img];
 }

The problem is that, it seems like the first image is perfectly set, but the rest of them are ignored... NSLogging them I can see that the images are there & the data is different every time, so, they are loaded ok, even I tried to addSubview to self (the class is UIImageView's child), through a UIImageView & they are showing, but this is not the way cause in that case I'm using too much memory... Asking for the description in the console, I can see how self.image = nil after setting it... I don't know what else I can do with this, it's driving me crazy.

Upvotes: 2

Views: 3453

Answers (1)

nano
nano

Reputation: 2521

Try to subclass to a UIView instead to a UIImageView, and create the UIImageView as a property inside.

@interface CustomView : UIView {
    UIImageView *imageView;

Then, in animationShowFrame use that imageView:

- (void) animationShowFrame: (NSInteger) frame {

    NSData *data = [animationData objectAtIndex:frame];
    UIImage *img = [UIImage imageWithData:data];
    [self.imageView setImage:img];
}

Hope it helps!

Upvotes: 4

Related Questions