I want to get image from a php file (for example: http://abc.xyz.com/api/showImage.php) for a specific user through the user id & set the image in the image view of my iOS app. How can i do so?
Thanks in advance.
You can get the image from the above URL in the form of NSData :
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL urlWithString:#"http://abc.xyz.com/api/showImage.php"]];
UIImageView *imageView = [UIImageView alloc] initWithImage:[UIImage imageWithData:imageData]];
Its better to use Asyn download method when displaying images from the web.
You could create your own category on UIImageView or use the popular ones out there
SDWebImage.
[imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
Store response into array and then into an object say profile Object.I am using SDWebImage.
if U are geting image url at detailsObj.profileImage.Then use like below to set to imageview..
NSLog(#"response is:%#",userDetailsArray);
ProfileObject *detailsObj=[userDetailsArray objectAtIndex:0];
//in place of detailsObj.profileImage use url server link which is getting image.
if([detailsObj.profileImage isEqualToString:#""] || [detailsObj.profileImage isKindOfClass:[NSNull class]] || detailsObj.profileImage.length==0)
{
self.profileImageView.image=[UIImage imageNamed:#"NoImage.png"];
}
else
{
//without using SDWebImage
[self.profileImageView setImageWithURL:[NSURL URLWithString:detailsObj.profileImage]];
//if it is NSData getting from server
self.profileImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL urlWithString:detailsObj.profileImage]]];
//or using SDWebImage
[self.profileImageView setImageWithURL:[NSURL URLWithString:detailsObj.profileImage] placeholderImage:[UIImage imageNamed:#"NoImage.png"]];
}
Related
I have imageView i want to load image from url but it is not loading i am using following code
pdfView.hidden=NO;
webView.hidden=NO;
pictureImageView.hidden=NO;
pdfView.backgroundColor=[UIColor clearColor];
doneButton = [[UIBarButtonItem alloc]initWithTitle:#" Done " style:UIBarButtonItemStyleBordered target:self action:#selector(doneButtonClick)];
self.navigationItem.leftBarButtonItem =doneButton;
webView.backgroundColor=[UIColor clearColor];
NSLog(#"File Name is %#",fileName);
NSLog(#"Enterning in the ImageSection");
NSString *ImageURL = fileName;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
pictureImageView.image = [UIImage imageWithData:imageData];
Instead of this if i want to give from local like below then also it does not show image in imageView any idea how to load image from url thank
pictureImageView.image=[UIImage imageNamed:#"starblue.png"];
First set frame of that image and then write this line after add image in pictureImageView
[self.view bringSubviewToFront:pictureImageView];
Code looks fine
Things to check
Url contain a valid image
Set ImageView alloc it properly set frame and then add image
UIImageView *pictureImageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 50, 50)];
Then do the rest
This is synchronous image loading that will block the main thread activities.Prefer to use asynchronous image download using AFNetworking or AsynchImageview
Use this code for show image in Url .
NSString *image=[NSString stringWithFormat:#"Your URL"] stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSLog(#"%#",image);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:image]];
Yourimageview=[UIImage imageWithData:imageData];
pictureImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgurl]]];
I am getting strange issue please if get help from anyone then it'll be appreciated.
My problem is i am getting only Image names from server through web service for ex: abc.png.
Now that image need to be attached with the static URL & than it will be used to display to UIImageView.
I have used this code:
for (int i = 0; i<[[myDic valueForKey:#"posts"] count]; i++)
{
NSData *bgImageData = nil;
if ([[[[myDic valueForKey:#"posts"] objectAtIndex:i] valueForKey:#"post"] valueForKey:#"video_image"] == [NSNull null])
{
bgImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://drupal.org/files/issues/no-pre.png"]];
}
else
{
bgImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://sitebildz.com/application/document/video/%#",[[[[myDic valueForKey:#"posts"] objectAtIndex:i] valueForKey:#"post"] valueForKey:#"video_image"]]]];
}
[arrVideoImage addObject:bgImageData];
}
For Display in Table :
cell.imageView.image = [UIImage imageWithData:[arrVideoImage objectAtIndex:indexPath.row]];
But don't know why its not working.
Thanks.
As commented The Tiger, your best choice would be to use an external library like SDWebImage which can handle download, caching and display of online images.
Here is an example using this framework :
[imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
You can also use blocks to insert logic once an image finished loading :
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
You're doing wrong. See you're making NSData in loop. Do you think it will wait until image is download from server ??? Nope it will not wait. So in this case you're adding empty NSData.
Would be better to use SDWebImage.
1. Download it
2. Import it in your project
3. build project (success)
4. Import #import "UIImageView+WebCache.h" in your ViewController class
[cell.imageView setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"placeHolderImageName"]];
you cab use https://github.com/enormego/EGOImageLoading/tree/master/EGOImageView which will download the image from server in async way and display once it completely downloaded from server. It also cache that image so that multiple request can be avoided and it enhances the system.
I have a NSMutableArray where it stores url to a image. And this NSMutableArray is called userPic. From my server it only prints out the url: not JSON or anything, only the url.
I can see in the output in Xcode that it shows the url, but how to I get this url in userPic to a background? Im trying to addSubView with the image for a background.
If something is unclear, sorry about that. Just let me know.
Please try to use this one.And i think your URL at index 0 of your array.
NSURL *imgURL = [NSURL URLWithString:[userPic objectAtIndex:0]]; // put your particular index where your image url in your array...
NSData *imgData = [[NSData alloc]initWithContentsOfURL:imgURL];
UIImage *img = [UIImage imageWithData: imgData];
[newView setBackgroundColor:[UIColor colorWithPatternImage:img]]
I am Trying to integrate SdWebImage With Youtube Api for the purpose of Fetching thumbnail image to cache them so as get rid of scrolling problem in UITableView.Problem is Youtube API Provide a URL that is for Thumbnail NSDATA which can be converted back to UIImage object but SDWebImage is Takes URL to an image to manage images in best efficient method.
Youtube Api Method:
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[thumbnails objectAtIndex:0] URLString]]];
UIImage * image = [UIImage imageWithData:data];
SDWebImage Method
[cell.imageView setImageWithURL:[NSURL URLWithString:[_objects objectAtIndex:indexPath.row]]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
Please Tell How We Solve this Problem.
I think you are overthinking this problem. You should be able to just do this:
NSURL *thumbnailURL = [NSURL URLWithString:[[thumbnails objectAtIndex:indexPath.row] URLString]]];
[cell.imageView setImageWithURL:thumbnailURL
placeholderImage:[UIImage imageNamed:#"placeholder"]];
Newbie to iPhone here.
I have an image to display fetched from a rss feed. I'm currently using the code below which works but slows down loading elements in the view:
for(int i=0; i<[bannerArray count]; i++){
NSString *bannerImagestr = [[bannerArray objectAtIndex:i] BannerImage];
bannerImagestr = [ bannerImagestr stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *banURL= [NSURL URLWithString:bannerImagestr];
NSData *data = [NSData dataWithContentsOfURL:banURL];
imgEventDetail = [[[UIImage alloc] initWithData:data] autorelease];
[banEventDetailArray addObject:imgEventDetail];
}NSLog(#"the banEventDetailArray is %#",banEventDetailArray);
I tried the SDWebImage api to make it load quick but i'm failing to get the image. The code which i've been using is below:
for(int i=0; i<[bannerArray count]; i++){
NSString *bannerImagestr = [[bannerArray objectAtIndex:i] BannerImage];
bannerImagestr = [ bannerImagestr stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *banURL= [NSURL URLWithString:bannerImagestr];
[banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
banImg=banIconImage.image;//<<<-------- updated here banImg is an instance of UIImage
[banEventDetailArray addObject:banImg];
}NSLog(#"the banEventDetailArray is %#",banEventDetailArray);
I need the banEventDetailArray in the form of UIImage, because I'm setting this array in the below code which takes (UIImage *) as its parameter
[eventsdetailroundedButtonType setBackgroundImage:[banEventDetailArray objectAtIndex:numTimerTicks] forState:UIControlStateNormal];
Please help me find what I've been missing out and where I may have gone wrong.
Thanks in advance.
UPDATE: i've replaced a line in the second code block. i.e. banImg=banIconImage.image; at line number 11.
You can use your image URL as key to find the cached image:
UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:[NSString \
stringWithFormat:#"%#",banURL]];
I guess the problem is in this lines:
[banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
banImg=[[UIImage alloc]init]; // <-- here: empty image
[banIconImage setImage:banImg]; // <-- here: overriding banURL image
since you are:
first, getting the image from the URL;
overriding that image with an empty one.
Use this code, instead:
[banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[banEventDetailArray addObject:banIconImage];
i.e., you add the image your retrieve to the array and then use it later.
You need to keep track of the UIImageView because it is where the SDWebImage magics happens. Indeed, that view handles the displays of the placeholder while the image is fetched; when the image is there, it is replaced.
You can get the image property only after the actual image is fetched, otherwise you will only get the placeholder.
Why are you setting the image twice? Doesn't make sense.
[banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
banImg=[[UIImage alloc]init];
[banIconImage setImage:banImg];
Try removing the second setting, should just be.
[banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
EDIT: Logic still doesn't make sense. You're probably trying add the UIImageView's image as an object in the array, which means your code should probably look like this.
NSString *bannerImagestr = [[bannerArray objectAtIndex:i] BannerImage];
bannerImagestr = [ bannerImagestr stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *banURL= [NSURL URLWithString:bannerImagestr];
[banIconImage setImageWithURL:banURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[banEventDetailArray addObject:banIconImage.image];
Try the code before you decide it's not what you need, apparently you don't exactly know what you want. If this is the answer to your question, mark it as the solution.
Your array does not hold image object. Instead it hold pointers (references) to UIImage objects. If you look at your working code, there you have:
imgEventDetail = [[[UIImage alloc] initWithData:data] autorelease];
[banEventDetailArray addObject:imgEventDetail];
You create and add new instance of imgEventDetail UIImage object with data and then add it to the array. You would need to do the same for your bottom (not working) code.
Try this in the not working code:
[banEventDetailArray addObject:banIconImage.image]; // array automatically retains.
All assuming that your images are downloaded correctly using SDWebImage API.