I am making an application just like a movie player but instead of showing a video, I have the movie in image frames. A slider is used to move the "movie" forward and backward and if released, the "movie" is auto played up to a chapter.
I accomplish this by having a UIImageView
and changing the image
property on each value change of the slider. After a while the app crashes due to memory warning. I cannot use the animation of UIImage because I want to control the position (aka visible image/frame) by scrubbing the slider.
So I have an ImageManager class that loads all the image names in string in an array (total 1500 images of about 150kb each) on init.
- (id) initWithCanvas:(UIImageView*) canvasView{
self = [super init];
if (self) {
totalImages = 1500;
self.canvas = canvasView;
self.images = [[NSMutableArray alloc] init];
for (int i=0; i<totalImages; i++){
[self.images addObject:[NSString stringWithFormat:@"frame_%04d.jpg", i]];
i = i + 4; // I use this to skip 4 frames
}
previewsImageIndex = -1;
[self setImageAtPosition:0];
}
return self;
}
Then I have a function that is fired on value change of the UISlider which finds the current image name and constructs a UIImage from the image name and sets it to the UIImageView
- (void) setImageAtPosition:(CGFloat)percentage{
currentImage = nil;
int position = percentage * [self.images count];
if (position != previewsImageIndex){
previewsImageIndex = position;
NSLog(@"Got image:%d at percentage: %f", position, percentage);
if (position < [self.images count]){
self.canvas.image = nil;
currentImage = [UIImage imageNamed:[self.images objectAtIndex:position]];
[self.canvas setImage:currentImage];
currentImage = nil;
//[currentImage release];
}
}
}
Even though I set the currentImage and the canvas.image as nil, still after a while the app crashes. I don't understand where am I having a memory leak. Also, I code with ARC, so no releasing.
Is there any other better way to do it, other than having a UIImage view? I want frame-by-frame view ability and the MPMoviePlayer or AVPlayer cannot do that fine-grained scrubbing.
imageNamed
because it will cache your images. – Mike WellercurrentImage
. – Marcus AdamsimageNamed
that did the bad stuff – Michael Kork.