Cannot programmatically show image in UIImageView (iPhone) - iphone

I'm developing a iPhone app, but I ran into a snag.
NSString *fileLocation = [[NSBundle mainBundle] pathForResource:#"happy.1" ofType:#"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation];
[self setImage:new_image];
if (new_image == nil) {
NSLog(#"can't load image happy.1.jpg");
}
bgImageView.image = image;
That's the code I'm using to show an image, but it doesn't appear! new_image isn't nil, which I understand it would be if it couldn't load the image. bgImageView is an IBOutlet and connected in IB.
Please tell me what I'm doing wrong. Thanks!
Edit: also, this does not work on the simulator nor device

Is image nil?
What's wrong with [UIImage imageNamed:#"happy.1.jpg"]?

So, usually this is because the images are not getting copied into the built executable. So, open the application package and see if they are there.
If then maybe check that fileLocation doesn't come back empty/nil.
I wonder if the ".1" is causing a problem. Try renaming you image file _1, _2 and see if that fixes it.

NSString *fileLocation = [[NSBundle mainBundle] pathForResource:#"happy.1" ofType:#"jpg"];
UIImage *new_image = [UIImage imageWithContentsOfFile:fileLocation];
bgImageView.image = new_image;

You can use
imageview.image=[UIImage imageNamed:#"imagename"];
and by using same we can show large number of images on each imageview.It will not cause the memory issue.just need to take care of proper release for allocated imageview.

Related

how to use gif images in UIImageView iOS

i have one gif image file having 4 images , i like to display these images in UIImage view one by one line image.animation . please guide is it possible to display or shoul i use alternat tell me .
thank you .
FLAnimatedImage is a performant open source animated GIF engine for iOS:
Plays multiple GIFs simultaneously with a playback speed comparable
to desktop browsers
Honors variable frame delays
Behaves gracefully under memory pressure
Eliminates delays or blocking during the first playback loop
Interprets the frame delays of fast GIFs the same way modern browsers do
It's a well-tested component that I wrote to power all GIFs in Flipboard.
it will help you , for gif link..https://github.com/mayoff/uiimage-from-animated-gif more help please feel free to ask
yes it possible in iOS,here i enclosed one URL below.please refer this for your concept,even i did it with help of this link.
https://github.com/mayoff/uiimage-from-animated-gif
hope i held you.
Try like this...
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"loading" withExtension:#"gif"];
UIImage *loadingImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
self.dataImageView.animationImages = loadingImage.images;
self.dataImageView.animationDuration = loadingImage.duration;
self.dataImageView.animationRepeatCount = 1;
self.dataImageView.image = loadingImage.images.lastObject;
[self.dataImageView startAnimating];
}
First of all you can add those 4 images in one Plist and do the following code. I hope it will work
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"PhotesArray" ofType:#"plist"]];
NSArray *imageNames = [picturesDictionary objectForKey:#"Photos"];
NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i=0; i<[imageNames count]; i++) {
[images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}
//Normal animation
self.dataImageView.animationImages = images;
self.dataImageView.animationDuration = 3.0;
[self.dataImageView startAnimating];

Crash on loading large number of images

I am creating an app in which I am loading 800 jpg images for the Imageview.On occurance of different enevts different set of images are to be loaded for animation.on fire of first event it works fine but on 2nd event it crashes on iPhone.Any help?
my part of code is as below
for (int aniCount = 1; aniCount < 480; aniCount++){
UIImage *frameImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat: #"ani_soft_whimper_%i", aniCount] ofType: #"jpg"]];
[_arr_ImagesSoftWhimper addObject:frameImage];
}
imageView.animationImages = _arr_ImagesSoftWhimper;
and i m getting crash for these set of images.
It is possible that you are running out of memory. But this is just a guess. Please provide some code.
1.if you are downloading from any server please call Asynchronous call.
2. check your image dimension,size,if dimension is big according to uiimageview frame then first resize or crop your image and then set into uiimageview.

Memory management UIImage ImageNamed

I m loading lots of rather large images in my viewcontroller, using
NSUInteger nimages = 0;
for (; ; nimages++) {
NSString *nameOfImage_ = #"someName";
NSString *imageName = [NSString stringWithFormat:#"%#%d.jpg", nameOfImage_, (nimages + 1)];
image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//some other stuff....
[imageView release];
}
the usual unloading occurs in - (void)viewDidUnload and - (void)dealloc
with self.image = nil; and [image release];
It seems after a few "loading" and "unloading" the cache still grows to the point of no return!!
:)
and the app crashes...
any ideas??? how do i empty my cache? and where?
thanks
EDIT:
ok this is what i was doing wrong.
Apparently this piece of code fixes the whole caching problem:
image = [[UIImage imageNamed:imageName] autorelease];
with autorelease being the key here.
thanks for the replies...
Thank you all for your suggestions.
Solution:
Used ARC and imageWithContentsOffFile to initialize the Images.
image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imageName ofType:nil]];
And Yes, imageNamed is only good for... well for nothing big...
image = [[UIImage imageNamed:imageName] autorelease];
This is incorrect. In keeping with the memory management rules, you shouldn't be releasing (or autoreleasing) the image because you didn't allocate or retain it. "imageNamed" doesn't contain "alloc", "new", "copy", or "retain".
As some of the other answers explain, you should load your images with a different method if you want more control over the memory they use.
imageNamed is an awful way to load images in reality, it never releases loaded images unless forced and keeps them in the cache forever. You should implement your own, more intelligent cache. A simple NSMutableDictionary gives the same functionality but with more flexibility.
For a more in-depth discussion you can read this: http://www.alexcurylo.com/blog/2009/01/13/imagenamed-is-evil/
Use another method to initialize you image. imageNamed caches.
Instead of using using imageNamed you can use imageWithContentsOfFile:
Or check this article
link 0
link 1

iphone sdk how to add uitableview cell image dynamically?

I am new to this iPhone development. Am developing a catalogue application. In that i need to give all product details with product images as tableview cell image dynamically since my images are coming from web service. I did this,
NSString* imageName = [NSString stringWithFormat:#"%#",[[stories objectAtIndex:indexPath.row]objectForKey:#"product_thumb_image"]];
cell.imageView.image.size.width==20;
cell.imageView.image.size.height==20;
cell.imageView.image = [UIImage imageNamed:imageName];
NSLog(#"imagename:%#",[[stories objectAtIndex:indexPath.row]objectForKey:#"product_thumb_image"]);
Am getting all the images in my console window and i checked that using nslog. But am not getting the images with simulator.What am doing wrong in this? Can anyone of you help me out?
Thanks in advance.
UIImage imageNamed: takes an NSString so if you want to use a formatted string like that use NSString's stringWithFormat
try:
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#",currentLink]];
As you are new to iPhone development I'll just expand that out to make it clearer (Ignore this if you already got what was happening there)
NSString* imageName = [NSString stringWithFormat:#"%#",currentLink];
cell.imageView.image = [UIImage imageNamed:imageName];
Do as follows:
NSString *imageName = [NSString stringWithFormat:#"%#",currentLink];
cell.imageView.image = [UIImage imageNamed:imageName];
Apple documentation contains an example of putting downloaded images into table cells. It's called LazyTableImages and would probably help with what you're trying to do.

save a photo library URL as an NSString then back to URL

what i am trying to do is save a URL to one of the photo library pictures. then use this URL to set the image on a CCSprite.
this is the URL being saved as a NSString :
[_currentTarget setObject:[[NSString alloc]initWithString:[[info objectForKey:#"UIImagePickerControllerReferenceURL"]absoluteString]] forKey:#"image1Name"];
Then set the a property of a Target object i made
NSString *image1PathName = [[listOfUserMadeTargets objectAtIndex:num]objectForKey:#"image1Name"];
self.normalImage = image1PathName;
this is when the CCSprite is made
Target *target = [[Target alloc] targetThatIsUserMade];
[_targets addObject:target]; // add it to the array
target.position = ccp(spawnX,spawnY);
target.scale = -0.001875 * target.position.y + 1.09375;
[self addChild:target z:1];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:target.normalImage]]];
[target setTexture:[[CCTextureCache sharedTextureCache] addCGImage:image.CGImage forKey:#"key" ]];
The problem im having is that the Target is there, but the image is not
I have done this many different ways with no luck
thank you for any help
This is a post (and answer) I made which shows you how to save the NSURL for a UIImage in the CameraRoll, and use it later to get the image back, using a UIImagePickerController. This sample code should also help you in how to access the image. Hope that Helps!