how to display images in custom size in uitable cell - iphone

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

Related

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

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.

unable to display image using SDWebImage

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.

how to fetch image from array and show it on table view

I am fetching image from server and save it on a NSMutableArray. The result of array like that
myarray={"roger_50.jpg",....};
Now i dont know how to access this image on table view.
Just have proper URL for image in array. And pass it to NSURL object as a link.
NSURL *imageURL = [NSURL URLWithString:[myarray objectAtIndex : yourindex]];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [[UIImage alloc] initWithData:data];
Have a nice coding.
Stay Hungry, Stay Foolish...
i hope the thing you are getting from array are only the names you can retrieve images from server in only two ways i) in nsdata ii)by url of the images on sever .By this way you cann't access the images.

UIImageView animation causing crash

I have a UIAnimation view that plays an array of PNG images as an animation. There are about 200 frames and total size is about 8 MB. The animation works just fine on simulator and iPhone 4, but when I test on iPhone 3GS, the app crashes due to the animation.
I've tried using UIImage imageNamed:, but I read that using imageWithData might be faster, so I have this:
NSString *imageName=[NSString stringWithFormat:#"fishBg_%i.png", i];
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
NSData *imageData = [NSData dataWithContentsOfFile:fileLocation];
[animationArray addObject:[UIImage imageWithData:imageData]];
What can my problem be? When I reduce the number of frames to about 100, then the animation plays and the app doesn't crash. But when I bring up the frame count to 200, then the app crashes. What's a better way to do this? The animation is a PNG sequence of transparent images, so I'm not sure if I'd be able to convert this to a video and keep its transparency and place other images under it.
Since we need to conserve as much memory as possible here (assuming that’s why you’re crashing), try managing memory more explicitly:
NSString *imageName=[[NSString alloc] initWithFormat:#"fishBg_%i.png", i];
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
[imageName release];
UIImage *theImage = [[UIImage alloc] initWithContentsOfFile:fileLocation];
[animationArray addObject:theImage];
[theImage release];