Gif Crashing on iOS device - ios

Gif was running smoothly on the simulator, but crashing in the device due to the memory. How to handle this. I have googled a lot but didn't find any kind of the solution to it.
The following is the code which I am using to load the Gif
NSURL *url = [[NSBundle mainBundle] URLForResource:[utility getString] withExtension:#"gif"];
self.img.image = [UIImage animatedImageWithAnimatedGIFURL:url];
Thanks in Advance

As per my knowledge It crashes due to memory pressure and high increase in memory usage.
check these links it may helps you link1 , link2!!and link3!!

Use FLAnimatedImage Library from Github :- Use https://github.com/Flipboard/FLAnimatedImage It is easy to use and memory friendly.
import "FLAnimatedImage.h"
import "FLAnimatedImageView.h"
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif"]]];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(0.0, 0.0, 100.0, 100.0); //As your Wish you can set frame
[self.view addSubview:imageView];
It's a well-tested component.

Related

Memory error when loading an image from a url in iOS today widget

I am trying to load an image from my server in my iOS Today Widget. However, whenever I do that, I get a crash that is because of a "memory error". I think it might be because I programmatically create the UIImageView, but then again that might not be the answer because loading a local image works fine. Is there a way to reduce the memory size while loading it?
In tableviewcell.m:
self.picct = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
self.picct.backgroundColor = [UIColor clearColor];
[self addSubview:self.picct];
In Today Widget Table View:
cell.picct.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.mywebsite.com/%#.jpg", [itemAtIndex objectForKey:#"myimagename"]]]]];
[cell.picct release];
Widget only can use 10MB(not documented, just guessed value from my experiments) memory due to prevent unload apps in background.

Cannot load an image from a remote url

I have tried many solutions for this problem before posting but somehow I didn't succeed.
I am using Xcode 6 and I am trying to add programmatically an image to my view. Everything is fine if I load an image beloging to the project, nothing works when I try to load an image resident on some remote server.
I have tried two different way:
1) using the libray SDWEBImage
UIImageView *imgg =[[UIImageView alloc] initWithFrame:CGRectMake(30, 250, 300, 20)];
NSURL *imageURL = [NSURL URLWithString:beaconLogo];
[imgg sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[self.view addSubview:imgg];
where beaconLogo is a string containing the url of the remote image (when I debug I can see that it has the correct value).
I have imported
#import "uiimageview+webcache.h"
#import "SDWebImageDownloader.h"
not sure if those are the correct headers needed. Anyway, the image is not displayed
2) I tried by using the following code that I found in some answer here on stackoverflow
NSURL *imageURL = [NSURL URLWithString:beaconLogo];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imgg =[[UIImageView alloc] initWithFrame:CGRectMake(30, 250, 300, 20)];
imgg.image = image;
[self.view addSubview:imgg];
No image is displayed, however I can see that there is something bad going on, because if I debug, I can see that "image" is nil, this line
UIImage *image = [UIImage imageWithData:imageData];
doesn't seem to have the desider effect.
Where am I going wrong? Thanks in advance.

ImageCaching on IOS

I've some trouble with memory on my app. I've checked Instruments to get more clue about this issue and i've found that 79% of my memory is used by this :
So i've searched on Google and some people said that is image caching which saved in memory all my images. Maybe it comes from my allocation ?
Here is how i call my images :
info = [InfoModel getInfo:[NSString stringWithFormat:#"%d", self.idEnigme]];
myImage = [UIImage imageNamed:[NSString stringWithFormat:#"res/img/%#", [info objectForKey:#"path1"]]];
myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame = CGRectMake(0, 200, [[UIScreen mainScreen] bounds].size.width, 400);
[self.scrollView addSubview:myImageView];
Info is a class where i parse a Json file where are my path to images.
Thanks for helping, this drives me crazy.
iOS automatically caches your image for future use when you call imageNamed:
As discussed in a few places, including here:Does UIImageView cache images?
You can get around this caching if you know you are only going to create it once by using
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]]
instead
I wouldn't worry about that too much, the UIImage cache is cleared when the app receives a low memory warning. It's all handled automatically, so any images that are no longer in use will be flushed from memory at this point.
So if your app is crashing from running out of memory it is not likely because the OS is caching images that are no longer in use.
You can handle your own caching of images by using initWithData instead of imageNamed but I doubt this solution will help you.
I'm very worried about the following screen. When i launch "Instruments" in Allocations mode, i see every image of my app adding to "Living". I really don't understand how it could be possible...
I've found the problem and i solved it. I'll explain you what was the problem, maybe it could help someone.
The real problem was my UIImage init. Initially i allocated my UIImage in method like the following code :
NSString* fileName = [NSString stringWithFormat:#"%#%#%#", name, number, ext];
UIImage *myImageHeader = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]];
UIImageView *myImageViewHeader = [[UIImageView alloc] initWithImage:myImageHeader];
myImageViewHeader.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 277);
[self.scrollView addSubview:myImageViewHeader];
Finally i've decided to create a UIImageview in my .h via a property and to set it in my method, like this :
#.h
#property (weak, nonatomic) IBOutlet UIImageView *headerImage;
#.m
[self.headerImage setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil inDirectory:nil]]];
By this way i saved a lot of memory but i still have leaks. So i decided to delete the image in my UIImageView after use :
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^(void){
self.headerImage.image = nil;
});
Now, my app only use 8mb of memory and manage memory correctly.
Thx for helping !

Image Optimization (iOS)

I'm working on a iPad Magazine and I'm using a lot of images (background, slideshow and animation) and the memory utilized is very high.
I've read the following method uses a lot of memory
UIImage *picture = [UIImage imageNamed:#"myFile.png"];
And they recommended using this one
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/myFile.png"];
imageView.image = [UIImage imageWithContentsOfFile:fullpath];
But I've found another method as well
imageView.image = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"myFile" ofType:#"png"]];
In order to optimize my app, which method should I use? All my images are .jpg and were saved for web in Photoshop.
All 3 methods will use the same amount of memory. The differences are the following:
Using [UIImage imageNamed:#"myFile.png"] image is cached in memory for faster reuse. This is good for small images used several times in your application (image background, etc). Cache is removed for non used images when memory warning is received.
Using [[UIImage alloc] initWithContentsOfFile:path] image is not cached and you can "force" release of memory by calling [image release] or setting property to nil using ARC. You have a better management of when memory is released
Using [UIImage imageWithContentsOfFile:fullpath] is just equivalent to [[[UIImage alloc] initWithContentsOfFile:path]autorelease]

Asynchronous image loading in iPhone?

Hi i'm new to iPhone application.In my application i used tableview.
when i click table cell it does to detail view which has UIScrollview. where i'm loading
images from the NSMutableArray. and in that NSMutableArray there are some URL images.
when i click table cell it takes more time to load all images.
please i need help to complete my application.Many people said to do asynchronously
but i don't know how to do.
thanks in advance. waiting for a solution .
You should probably use a library such as AFNetworking to load your images, it handles downloading and swapping in the image on the tableView.
Firstly I would like recommend you Paul Hegarty tutorials (in Lectures 9, 10 and mainly 11 (multithreading)) you can learn too much.
Here some sample code can help you quickly but superfluously:
dispatch_queue_t imageFetchQ = dispatch_queue_create("image fetcher", NULL);
dispatch_async(imageFetchQ, ^{
NSData *imageData = [[NSData alloc] initWithContentsOfURL:self.imageURL]; // could take a while
// UIImage is one of the few UIKit objects which is thread-safe, so we can do this here
UIImage *image = [[UIImage alloc] initWithData:imageData];
// check to make sure we are even still interested in this image (might have touched away)
if (self.imageURL == imageURL) {
// dispatch back to main queue to do UIKit work
dispatch_async(dispatch_get_main_queue(), ^{
if (image) {
self.scrollView.zoomScale = 1.0;
self.scrollView.contentSize = image.size;
self.imageView.image = image;
self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
}
});
}
});

Resources