Can we assign the value of an object of an array having an image value to a variable of image view, see the following code
NSArray *imgArray=[[NSArray alloc] initWithObjects:#"Bingo2.png", nil];
UIImageView *img=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
img.image=[imgArray objectAtIndex:0]; //line 3
[self.view addSubview:img];
its not working, Application is crashing i guess because of line 3
Please help me,
Many Thanks for the help.
You are storing an NSString object in the array and not an image. That is why it is crashing.
use this
img.image= [UIImage imageNamed:[imgArray objectAtIndex:0]];
instead of
img.image=[imgArray objectAtIndex:0];
if the above doesn't work
than you can also use this
NSString *string = [NSString stringWithFormat:#"%#", [imgArray objectAtIndex:0]];
img.image= [UIImage imageNamed:string];
You have a string of a filename in your array, not an image. Use [UIImage imageNamed:#"Bingo2.png"] instead.
Related
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.
I have a window in which I want to display an image in a specific block of that window. The image is retrieved from the local XML file. I parsed the XML file and got the image value in the log, but I don't know how to put it in that specific block. The below image pasted for the reference.
The arrow mark represents that within this UIImageView I need to get the image. Also, I doubt that I need to mention any name for reference to UIIMageView in storyboard. Suggest me an idea.
Edited
My parser code:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Sample.xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data];
[xmlParser setDelegate:theParser]
After this I am using this code for getting image:
UIImage *image = [UIImage imageWithContentsOfFile:#"/Users/administrator/Desktop/imageApp/resources/Main_pic1.png"];
After this I don't know how to continue.
Yes, you have to create IBOutlet in .h file
like IBOutlet UIImageView *imgview; and connect imgview with your UIImageview from xib.
After that you can set image to image view.
NSURL *URL = [NSURL URLWithString:[your url which u get form local xml]];
NSData *imageData = [NSData dataWithContentsOfURL:URL];
UIImage *image = [UIImage imageWithData:imageData];
imgview.image=image;
Ttry this, It may help you.
You can directly pass the url image to Uiimageview by :
YourImageView.image = [UIImage imageWithContentsOfURL:YourUrl];
It's possible to do the equivalent by going through NSData, but it's a bad idea. Instead, the image should be downloaded asynchronously using NSURLConnection, and only once it's fully downloaded should it be converted into a UIImage and assigned to the image view.
Save all your urls in an say NSMutableArry *UrlArray then
int x = 0;
int y = 10;
for (int i = 0; i< [UrlArray]; i++)
{
UIImageView *imageView = [[[UIImageView alloc]initWithFrame:CGRectMake(3+x,y, 80,
80)]autorelease];
imageView.backgroundColor = [UIColor blueColor];
if (x >= 300)
{
x = 0;
y = y+imageView.frame.size.height+3;
imageView.frame = CGRectMake(x+3 , y, 80, 80);
}
CIImage *beginImage = [CIImage imageWithContentsOfURL:[UrlArray ObjectAtIndex:i]];
imageView.image = [UIImage imageWithCIImage:beginImage];
x = x+imageView.frame.size.width+5;
[self.view addSubview:imageView];
}
will do the work i guess;
I am trying to show various images from a plist file into UIImageView , I wrote my code like this :
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Photos" ofType:#"plist"]];
imageArray = [picturesDictionary objectForKey:#"PhotosArray"];
PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
But actually nothing happens, and my ImageView is empty ! also I have two buttons to forward backward images .
Contents of Plist :
The plist is incorrect, it should be
Root ---- Dictionary
PhotosArray ----- Array
Item 0 ------ String -- Beach.jpg
Then add this code to your viewController.. Be sure that the Interface imageView is link to IBOutlet.
NSDictionary *picturesDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"Photos" ofType:#"plist"]];
NSArray *imageArray = [picturesDictionary objectForKey:#"PhotosArray"];
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:0]];
PhotosInAlbum.image = [UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
this part should be like this
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];
Try using this:
PhotosInAlbum.image = [UIImage imageNamed:[imageArray objectAtIndex:1]];
Your [imageArray objectAtIndex:1] will only return image name not its path, so in order to use imageWithContentsOfFile you have to specify the path of your image not only image name.
imageWithContentsOfFile: use a path that is the full or partial path to the file.
You can use imageNamed: method instead (which use the name of the file). Just replace
[UIImage imageWithContentsOfFile:[imageArray objectAtIndex:1]];
to
[UIImage imageNamed:[imageArray objectAtIndex:1]];
initially, [UIImage imageNamed:[imageArray objectAtIndex:1]]; would have solved ur problem.
But you are not able to resolve this issue this way which looks like
the problem seems to be with this line
[controller.view addSubview:PhotosInAlbum];
it might be the case your controller.view is nil at the moment you are trying to add ur imageview.
just make sure it isn't. cos if it is then your PhotosInAlbum is not at all getting added as subview to the controller.view.
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.
I'm populating the animationImages field of my UIImageview via NSArray arrayWithObjects. When I put objects in the array via UIImage imageNamed: the UIImageview animates as expected. However when I put objects in the array via UIImage alloc initWithContentsOfFile I don't see the animated images (I just see the default image I pass to initWithImage for the UIImageview). Any ideas?
Cheers!
That is because imageNamed: and initWithContentsOfFile: both look in different directories. imageNamed: looks in your bundle directory, initWithContentsOfFile: does not.
If you have code like this:
NSString *file = #"image.png";
UIImage *image = [UIImage imageNamed:file];
But you want to use initWithContentsOfFile:, you should use this code:
NSString *file = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:file];