Cannot load an image from a remote url - ios

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.

Related

Gif Crashing on iOS device

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.

How can parse all the images form Website in IOS SDK

I like to implement image upload functionality in my app.For that I like to give some option for user to choose image.
One of the option is choose image from website.
If the user enter an url link in the text box and click the get images button means. I like to display all the images in that particular link
But I am not having any idea about this.
I have explain my requirement below
1)Enter the url in a text box
2)Click the get images button
3)Parse all the images from that particular url
4)Display all the images in the Imageview
And also to illustrate my requirement I have attached the screenshot below
If anybody work around this functionality means please suggest me some idea
Thanks
Like this ?
- (IBAction)buttonClicked:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.yourwebvsite.com/image.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData: data];
UIImageView *displayImageView = [[UIImageView alloc] initWithImage:image];
[displayImageView setFrame:CGRectMake(0, 0, 300, 300)];
[displayImageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:displayImageView];
}
Or if you want all the images from a web directory. See here and save all the images in an array then display the images in a UIImageView like my code above.

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 retrieved from url does not fit properly into UIImageView

I'm trying to download an image from an url and display into a cell (sample below):
NSURL *url = [NSURL URLWithString:#"http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"];
NSData *data = [NSData dataWithContentsOfURL:url];
[cell.imageView setImage:[UIImage imageWithData:data]];
Somehow when I display into the cell, it covers the whole cell, bigger than my UIImageView.
But If I display the image locally like below:
[cell.imageView setImage:[UIImage imageNamed:#"defaultProfile.png"]];
It fits perfectly into the UIImageView I set into the cell.
Why is this the case?
use scaleToFit property of imageview

Fastest way to load images into a UIView?

i have a UIScrollView which holds different UIView container. Each container has to load six UIImages. When scrolling i need to adjust the contents within the "layoutSubviews" method.
Unfortunately loading these images affects in hiccups when the new container and the images will be created.
I´m loading the images async via "dispatch_async". I also used small 32x32px thumbnails which will be replaced with the full image 256x256px image. Unfortunately its stuttering..
Here´s the code of the "initWithFrame" method of such a UIView.
These UIViews will later be added as a subview to the UIView container.
// load ThumbnailImage and replace it later with the fullImage
NSString* thumbImageName = [self.data.imageName stringByAppendingString:#"_small"];
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: thumbImageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: self.data.imageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
});
});
Is this the faster way to load images into a UIView?
How does Apple implemented the really fluid image view inside the "Photo.app" when scrolling between different fullscreen images?
I don´t know if it would make sense to use CoreImage?
Any advice would be great!
Thanks for any help and your time.
Have a nice day.

Resources