ImageView loads local image but not from URL - ios

I am having trouble getting an image to load from a URL. I have CollectionViewCell subclass with an image view setup like this:
#property (strong, nonatomic) IBOutlet UIImageView *albumCover;
I cannot get it to load images from a URL. Here is the code im using:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
cell.albumCover = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
Meanwhile, this works:
cell.albumCover.image = [UIImage imageNamed:#"lock.png"];
I know the URL connection is getting the proper image back because I am monitoring it with Fiddler. Why isn't the image showing up then?

I was allocating the UIImageView object again instead of just initializing it with the image. The correct code is:
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: song.albumURL]];
[cell.albumCover initWithImage:[UIImage imageWithData: imageData]];

I use SDWebImageView:
Asynchronous image downloader with cache support with an UIImageView category
http://hackemist.com/SDWebImage/doc

You should use fileURLWithPath: instead of the URLWithString:.
URLWithString: is for WWW URL.
And an example:
NSString *rootPath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [rootPath stringByAppendingPathComponent:#"example.mov"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
yourMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: fileURL];

Related

How to align an image in the center in WKWebView objective c

Here is my code to display image.
NSString *doc = #"http://13.232.1.87:3000/images/flights/bc4952c8ec51588cdbd72d91c4e1533562273837.jpeg";
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:doc]];
[webView loadRequest:nsrequest];
[self.view addSubview:webView];
I see your link points to a simple jpg image.
You might then feed the WKWebView with html code instead of the direct link to the image, using
[webView loadHTMLString:#"<html>...</html>" baseURL:nil];
[edit] Or, if the link is always pointing to an image, you can consider not even using the WKWebView replacing it with a UIImage:
NSURL *url = [NSURL URLWithString:doc];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
or, for shortness:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:doc]]];
And then set the image frame (or constraints) like you would with the WKWebView

Xcode tableview cell unable display image via url

I followed the code based on here tableview Tutorial and i did successfully show something in my tableview.
I want to change the cell image by using url link from website.
Here is my code:
NSString *str = [NSString stringWithFormat:#"http://piq.codeus.net/static/media/userpics/piq_126382_400x400.png"];
NSString *path = [str stringByReplacingOccurrencesOfString:#"," withString:#"/"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
If I change my str to
NSString *str = #"https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSrKOobbDUVMwrrqMkpvfhITeyQewM8Kj0fZg_X_u3TaM-CLi5QfA";
then it will successfully change the image of each cell in table view.
Both urls link to the same image but the first link image size is bigger than second link.
Problem solved. Thanks to #GoodSp33d.
For those who are using Xcode 7. Please refer to this solution.
Here is my updated code:
NSString *str = [NSString stringWithFormat:#"your image url"];
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL : url];
cell.imageView.image = [UIImage imageWithData:data];

Image fetched from webservice is not displaying in viewcontroller

I tried fetching the image location but its not displaying it in UIImageView, the code which is used for fetching is,
in class.h file,
#property (strong, nonatomic) IBOutlet UIImageView *imageview;
class.m file,
NSString *imagee;
imagee = [result objectForKey:#"image"];
NSLog(#"image is %# \n",imagee);
the 'imagee' string could return the exact url for the image.
code for image displaying is,
UIImage *imagevieww = [UIImage imageNamed:[result objectForKey:#"image"]];
imageview.image = imagevieww;
but at final i could get the empty space where the image should be placed.
Found the answer for my problem, thanks for everyone who helped me to find this answer.
UPDATED: Include 'http' with the urlstring so that, it can fetch the correct image from the webservice.
NSString *urlString = [NSString stringWithFormat:#"http://%#",[result objectForKey:#"image"]];
NSLog(#"url image is %# \n",urlString);
NSData *imageData = [[NSData alloc] init];
imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
NSLog(#"image data is %#",imageData);
imageview.image = [UIImage imageWithData:imageData];
The UIImage method imageNamed: is used to load image present locally. In your case you need to download the image from the URL. There are various ways to do this based on requirement.
Easiest one is,
//URL encode the string
NSString *url = [[result objectForKey:#"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
imageview.image = [UIImage imageWithData:imageData];
This will download the image synchronously and on the main thread(if called from main thread). You want to do this asynchronously on background thread. Usually dispatch_async(GCD) is used for doing this.
Hope that helps!
If 'imagee' returned exact url , then fetch the image from web service should be like this.
imageview.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:#"image"]]]];
Try this
NSString *imageString = [NSString stringWithFormat:#"data:image/jpg;base64,%#",[yourDict objectForKey:#"ImageString"]];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]];
UIImage *image = [UIImage imageWithData:imageData];
.h
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
.m
Get Image From Webservices:
NSURL *url = [NSURL URLWithString:#"http://www.site.com/image.jpg"];
NSData *myImageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:myImageData];
Set Image to ImageView:
imageView.image = image;
I did the following and it works for me.
NSURL *url = [NSURL URLWithString:#"http://www.yourdomain.com/imagename.png"];
NSData *myData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:myData];
["%storyboardImageName%" setImage:image];
Please note that storyboardImagename is the variable that you put on the xib or storyboard. You will need to change it. Also note the 'setImage' attribute which actually ties it together.
Hope this helps.
Convert the URL into UIImage,
Try This:
NSURL *url = [NSURL URLWithString:#"http://yoursite.com/imageName.png/jpg"];
NSData *myData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[[UIImage alloc] initWithData:myData] autorelease];
Use this image on imageview. If you are getting problem then try to download the image in background.
Follow this Link

UITableViewCell images not loading ios

I am trying to download images asynchronously and display them in UITableViewCell imageView. The problem is that my NSURL is nil for some reason..
NSString *urlString = feed[#"imageURL"];
NSLog(#"urlString %#", urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"url image %#", url);
This will print something like
urlString http://www.....image.jpg
url image (null)
Can someone please tell me why this is happening? Thanks.
What your doing is getting the contents within a url with NSURL. Once you have the contents of the url, you are basically saying to the compiler, load these SOMETHING contents from this url. But that SOMETHING whether it's an image or json object or whatever, needs to be loaded into an NSData object then load the NSData object into a UIImage object. Now you can just say cell.imageView.image = theUIimage for your exact problem but heres a simple example that just loads such UIImage into a UIImageView like so:
NSURL *URL = [NSURL URLWithString:#"http://l.yimg.com/a/i/us/we/52/37.gif"];
NSData *data = [NSData dataWithContentsOfURL:URL];
UIImage *img = [[UIImage alloc] initWithData:data];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
imageView.image = img;
[self.view addSubview:imageView];
Please no downvote :)

load video from File Path

i am trying to Load video from Path in my file system Project , i have code for the URL stream
NSString *path = [[NSBundle mainBundle] pathForResource:#"US_Very_High_Dive_Boudia_US_44_x264" ofType:#"mp4"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
You don't. MPMoviePlayer knows best when to load data. Convert the file path to a NSURL, or even better, just use
NSURL *myURL =`[[NSBundle mainBundle] URLForResource:#"US_Very_High_Dive_Boudia_US_44_x264" withExtension:#"mp4"];`
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: myURL];

Resources