uiimageview is not Working using URLWithString: - ios

_profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#".../images/Profile/Pratik Maniya Photo.jpg"]]];
I want to display image using the URL link in the code,
but there is not any result.

The problem is in the spaces symbols in url.
You can use:
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]
Full code:
NSString *base_url = #"https://safesocpgm.000webhostapp.com/images/Profile/Pratik Maniya Photo.jpg";
NSString* encoded_url = [base_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
_profileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:encoded_url]]];

Related

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];

Parse json data image to image view

This is my JSON response,
{
"AppConfig": {
"store_logo": "url",
"deal_status": "A",
"see_the_menu_btn": "A",
"store_id": "3",
"store_name": " Pizza Restaurant",
"bg_image": "www.my image.png"
}
}
NSString *localwthr = [NSString stringWithFormat:#"my url"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:localwthr]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (responsedata) {
NSDictionary *Dictionarydetails = [NSJSONSerialization
JSONObjectWithData:responsedata
options:kNilOptions
error:nil];
NSLog(#"The return data is: %#",Dictionarydetails);
NSString *imgURL=[[Dictionarydetails objectForKey:#"AppConfig"]objectForKey:#"bg_image"];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];
}
}
I need to get URL value for key bg_image, download image and set it in the UIImageView. How can I do this?
imageUrl is never like "www.my image.png",
imageUrl is like "http://www.serverName.com/directory/..../imageName.png"
if there is space in your url then you have to convert it into UTF8 format, which is a standard format for webURL.
so You should use,
imgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//Use it as it shown in below code.
NSString *imgURL=[[jsonDict objectForKey:#"AppConfig"]objectForKey:#"bg_image"];
imgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];
cheers!
For loading the image from the URL, then
NSString *imgURL=[[jsonDict objectForKey:#"AppConfig"]objectForKey:#"bg_image"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];
Try This:
NSString *imgURL=[[jsonDict objectForKey:#"AppConfig"]objectForKey:#"bg_image"];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,100,100)];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imgURL]]];
check out this code , is this you are needed or not as rmaddy told post your done code as far as now
NSString *imageName=[[jsonDict objectForKey:#"AppConfig"]objectForKey:#"bg_image"];
imageview.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString: imageName]]];

Display image from url in ios

I added UIImageView in my xib, connected its IB named 'codeImage' to the file owner, synthesized in .m and autolayout is unchecked.
NSURL *urlForImage = [NSURL URLWithString:#" url "];
NSData *dataImage = [NSData dataWithContentsOfURL:urlForImage];
UIImage *imageData = [UIImage imageWithData:dataImage];
self.qrCodeImage.image = imageData;
Couldn't display image. Is there something I missed?
there was an unused UIImage *image = nil and I set [qrCodeImage setImage:image]; rite at the end of the viewDidLoad.. Code in the question itself is correct.
NSString *str=[[NSString alloc]initWithFormat:#"http://bwwapp.com/mvbritt/toolimg/mobilethumbs
/%#",[[getOneByOne objectAtIndex:path.row] valueForKey:#"iimg"]];
UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:str]]];

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

NSURL with Pipes |

I'm trying to initialize an UIImage via URL.
This is the code I wrote:
NSString * path = [NSString stringWithFormat:#"%#",[[_tours objectAtIndex:indexPath.row]objectForKey:#"map_url"]];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
[[cell tourPreviewImage]setImage:img];
....
An example map_url looks like the following:
https://maps.google.com/maps/api/staticmap?size=200x200&maptype=roadmap&markers=size:big|color:green|72.3601030185,41.4626763775&markers=size:big|color:red|45.4563468516,94.4774796973&sensor=false
It seems that the NSURL Object cant handle the | Pipe Character the right way because i dont get any image data. If I delete the parameters separated by the pipe out of the url everything works fine.
Any Ideas how to solve this problem?
ok i found a solution for this. Encoding is what did go wrong.
NSString * path = [NSString stringWithFormat:#"%#",[[_tours objectAtIndex:indexPath.row]objectForKey:#"map_url"]];
path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc]initWithData:data];
[[cell tourPreviewImage]setImage:img];

Resources