unable to display image using SDWebImage - iphone

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.

Related

Display Image to UIImageView which comes from Server

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.

iPhone NSMutableArray loading background image from url

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

Get image from a php file to iOS

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

how can I display UIImage from image url

The code below works fine if I give statically but
imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:#"http://4cing.com/mobile_app/uploads/pageicon/6.jpg"]]]];
Same code - I am passing the image url name dynamically it's not working
imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:#"%#",[imagename objectAtIndex:0]]]];
You need to verify the NSArray contents. Log out the objects of the array:
NSLog(#"imagename = %#",[imagename description]);
Also, a side-note - might want to think about loading that image dynamically. I wrote a class that makes this pretty painless:
https://stackoverflow.com/a/9786513/585320
The answer is geared toward using in TableViews (where lag would really hurt performance), but you can (and I do) use this for any web image loading. This keeps the UI fluid and is very fast.
NSMutableArray *imagename = [[NSMutableArray alloc] initWithObjects:#"http://4cing.com/mobile_app/uploads/pageicon/6.jpg", nil];
UIImageView *imgDescView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 200, 200)];
imgDescView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:[NSString stringWithFormat:#"%#",[imagename objectAtIndex:0]]]]];
it is working ... i think u r not creating array properly.
Hi create url in this manner
[NSURL URLWithString:[[NSString stringWithFormat:#"%#",[imagename objectAtIndex:0]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
i think it will work and will resolve ur problem.

how to display images in custom size in uitable cell

i my iphone applicaion image in right of table cell are extracted from xml feed. Know i want to display them in fix size can any body help me out.
bellow is the code
int blogEntryIndex1 = [indexPath indexAtPosition: [indexPath length] -1];
imgstring=[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: #"image"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
cell.imageView.image=img;
cell.textLabel.text=headline;
Thanks in advance i will be waiting
Set the content mode of your UIImageView to UIViewContentModeScaleAspectFit or to UIViewContentModeCenter if your images do not require any scaling.
[yourImageView setContentMode:UIViewContentModeScaleAspectFit];
Take a look at the examples in the API. There is a better way to load images for table view cells.
http://developer.apple.com/iphone/library/samplecode/LazyTableImages
And for a better understanding of the anatomy of table view cells read this:
http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html