Crash on loading large number of images - iphone

I am creating an app in which I am loading 800 jpg images for the Imageview.On occurance of different enevts different set of images are to be loaded for animation.on fire of first event it works fine but on 2nd event it crashes on iPhone.Any help?
my part of code is as below
for (int aniCount = 1; aniCount < 480; aniCount++){
UIImage *frameImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: #"ani_soft_whimper_%i", aniCount] ofType: #"jpg"]];
[_arr_ImagesSoftWhimper addObject:frameImage];
}
imageView.animationImages = _arr_ImagesSoftWhimper;
and i m getting crash for these set of images.

It is possible that you are running out of memory. But this is just a guess. Please provide some code.

1.if you are downloading from any server please call Asynchronous call.
2. check your image dimension,size,if dimension is big according to uiimageview frame then first resize or crop your image and then set into uiimageview.

Related

how to use gif images in UIImageView iOS

i have one gif image file having 4 images , i like to display these images in UIImage view one by one line image.animation . please guide is it possible to display or shoul i use alternat tell me .
thank you .
FLAnimatedImage is a performant open source animated GIF engine for iOS:
Plays multiple GIFs simultaneously with a playback speed comparable
to desktop browsers
Honors variable frame delays
Behaves gracefully under memory pressure
Eliminates delays or blocking during the first playback loop
Interprets the frame delays of fast GIFs the same way modern browsers do
It's a well-tested component that I wrote to power all GIFs in Flipboard.
it will help you , for gif link..https://github.com/mayoff/uiimage-from-animated-gif more help please feel free to ask
yes it possible in iOS,here i enclosed one URL below.please refer this for your concept,even i did it with help of this link.
https://github.com/mayoff/uiimage-from-animated-gif
hope i held you.
Try like this...
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"loading" withExtension:#"gif"];
UIImage *loadingImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
self.dataImageView.animationImages = loadingImage.images;
self.dataImageView.animationDuration = loadingImage.duration;
self.dataImageView.animationRepeatCount = 1;
self.dataImageView.image = loadingImage.images.lastObject;
[self.dataImageView startAnimating];
}
First of all you can add those 4 images in one Plist and do the following code. I hope it will work
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"PhotesArray" ofType:#"plist"]];
NSArray *imageNames = [picturesDictionary objectForKey:#"Photos"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i=0; i<[imageNames count]; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
//Normal animation
self.dataImageView.animationImages = images;
self.dataImageView.animationDuration = 3.0;
[self.dataImageView startAnimating];

How do I read in & display a local image on UIImageView via SQLite

How may I modify, read in an image path from SQLite DB, & display it via an UIImageView? In other words, this application will work in offline mode & i have the images at hand. How will i go about this then, tgt with sqlite? (retrieve images by storing their image path name # sqlite)
Sample code I have for now:
- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (SQLAppDelegate *)[[UIApplication sharedApplication] delegate];
//start with index 0
currentStepIndex = 0;
Resolution *resolutionObj = [appDelegate.resolutionArray objectAtIndex:currentStepIndex];
[resolutionDescription setText:resolutionObj.stepDescription];
//checking for null string
//replicate of ifelse #goToNextStep & #goToLastStep (needed for initial load of image)
if (resolutionObj.imageDescription != NULL)
{
//if goes in here, then image description is not null
NSURL *imageURL = [NSURL URLWithString:resolutionObj.imageDescription];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
[resolutionImage setImage:[UIImage imageWithData:imageData]];
}
else
{
//if goes in here, then image description is NULL
//show empty image
[resolutionImage setImage:NULL];
}
}
You are missing a lot the way I see it. If your images are coming from a website then you should use a webView to display it. If you are planning to display images in UIImageView, you should first download them onto your device from the url that points to that image and save it in the sandbox. Then you store the path of this downloaded image in your sqlitedb. Now when you want to load this image you just access this image using the path that you have in your sqlitedb and render it. How do you think by just saving the imageName image013.png and not downloading the image itself will render the image for you in UIImageView. Just think where your UIImageView will go and find this image if it's not already downloaded onto your device. For going and finding on the web directly, you need a UIWebView and not an UIImageView
If you are packaging your images as resources with your app binary, you can load them as shown below:
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myimagefile" ofType:#"png"]];
[self.testView.imgView setImage:img];
where myimagefile is the name of your file and ofType is the type of image. .png or .jpg or whatever.
If you have anymore doubts. Please come back.

Animated-GIF images received as NSData through NSSocket unable to split frames

I'm having trouble with the way iOS handles animated-GIF's. I know that you cannot use animated-GIF's on an UIImageView and you have to use custom animations on the UIImageView.
But..
I have a Java server that sends GIF images through a socketstream. The iOS (iPhone) receives that stream and converts it into an NSData type. I've succeeded in capturing and displaying this image in a UIImageView but as many of you already know.. it only displays the first frame.
I Also found some code to decode that GIF into separate images, but that code works from a GIF file and not NSData.
Question: How do i convert the NSData file into separate images and place them in an NSArray to use it as animation?
Note: In the NSData that is received are both an image and some text separated by a rare character. so the NSData looks like this: [image] [separator] [text].
Hope somebody can give me some pointers or some samples to work with..
Thanks in advance, i will keep searching untill you or me finds an answer :)
Unless you're targeting devices before IOS 4, use ImageIO.
NSMutableArray *frames = nil;
CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL);
if (src) {
size_t l = CGImageSourceGetCount(src);
frames = [NSMutableArray arrayWithCapacity:l];
for (size_t i = 0; i < l; i++) {
CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
if (img) {
[frames addObject:[UIImage imageWithCGImage:img]];
CGImageRelease(img);
}
}
CFRelease(src);
}
I made a wrapper that also handles animation time based on the code from Anomie
https://gist.github.com/3894888

Cannot programmatically show image in UIImageView (iPhone)

I'm developing a iPhone app, but I ran into a snag.
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:#"happy.1" ofType:#"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation];
[self setImage:new_image];
if (new_image == nil) {
NSLog(#"can't load image happy.1.jpg");
}
bgImageView.image = image;
That's the code I'm using to show an image, but it doesn't appear! new_image isn't nil, which I understand it would be if it couldn't load the image. bgImageView is an IBOutlet and connected in IB.
Please tell me what I'm doing wrong. Thanks!
Edit: also, this does not work on the simulator nor device
Is image nil?
What's wrong with [UIImage imageNamed:#"happy.1.jpg"]?
So, usually this is because the images are not getting copied into the built executable. So, open the application package and see if they are there.
If then maybe check that fileLocation doesn't come back empty/nil.
I wonder if the ".1" is causing a problem. Try renaming you image file _1, _2 and see if that fixes it.
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:#"happy.1" ofType:#"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation];
bgImageView.image = new_image;
You can use
imageview.image=[UIImage imageNamed:#"imagename"];
and by using same we can show large number of images on each imageview.It will not cause the memory issue.just need to take care of proper release for allocated imageview.

Preload Images in iPhone application?

I have a sequence of images needed to display in a short time (PNG sequence). There are totally 31 PNGs in total, each with file size about 45KB. I have already loaded them with the following codes:
imgArray = [[NSMutableArray alloc] init];
for(int i = 0; i <= 30; i++) {
NSString * filename = [NSString stringWithFormat:#"img_000%.2d.png", i];
UIImage *temp = [UIImage imageNamed:filename];
[imgArray addObject:temp];
[temp release];
temp = nil;
}
I use the following codes for displaying the images:
CGImageRef image = [(UIImage *)[imgArray objectAtIndex:imgFrame] CGImage];
imgLayer.contents = (id)image;
if(imgFrame < 29) {
imgFrame++;
} else {
imgFrame = 0;
imgLayer.hidden = TRUE;
[imgTimer invalidate];
}
where imgLayer is a CALayer. (imgTimer is a repeating timer with interval 0.03s)
But I found that when I call the images out, it is very laggy at the first time. Except the 1st appearance, other appearance has no problem.
Is it related to preloading images? Or are my images too big in file size?
The reason for your lags are hard to tell without profiling data. But here is a trick that might help: Join all your images into one large file. Try to make it rectangular (maybe 6x6 in your case or 4*8). Then load this single file and crop each image out for display (i.e. create an image for display with the size of a tile and copy one tile from the big image after the other into the display image).
Images are loaded when they are used and displayed.
If you use the time profiler in Instruments, you will the lag you're experiencing. If you then zoom to that lag and look at what is causing it, you will usually see that "copyImageBlockSetPNG" is the function taking time, right before "inflate".
What you must find a way to do is create your images and force load them before you need them. That's another story apparently.