2
votes

Well, I have an UIImageView that I want to animate its images via "animationImages" as you can see here:

UIImage *img1 = [[UIImage imageNamed:@"image1.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 150, 20, 150)];
UIImage *img2 = [[UIImage imageNamed:@"image2.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 150, 20, 150)];
self.imgStatus.animationImages = [NSArray arrayWithObjects:img1, img2, nil];
self.imgStatus.animationDuration = 1;
self.imgStatus.contentMode = UIViewContentModeScaleToFill;
[self.imgStatus startAnimating];

The width of the two images is smaller than de width of the UIImageView, so I want that these images being resized when the animation displays them. However the animation don't make any rezise with "resizableImageWithCapInsets", and just scale them to fill the UIImageView's frame.

Any idea about it? How can I do it?

Thanks!!

1

1 Answers

1
votes

Well, the solution I've found is that all UIImages of the array have to be resized before starting the animation. So, I create this category of UIImageView with a procedure, similar to setter "setAnimationImages", but inside it all UIImages are resized by UIImageView frame. Also the "ContentMode" property of UIImageView is set as "UIViewContentModeScaleToFill".

Finally, it's very important that every UIImage previously sets "resizableImageWithCapInsets" to know how to resize each one.

Example:

#import "UIImageView+AnimationImages.h"

UIImage *img1 = [[UIImage imageNamed:@"image1.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 150, 20, 150)];
UIImage *img2 = [[UIImage imageNamed:@"image2.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 150, 20, 150)];

[self.imgView setAnimationImagesWithUIImageViewSize:[NSArray arrayWithObjects:img1, img2, nil]];

self.imgView.animationDuration = 1;

[self.imgView startAnimating];

You can find this category (UIImageView+AnimationImages) here