Show an image from json - objective-c

I'm not able to show an image that I load from a json file.
I'm parsing my json with JSONKit and everything works fine but I can't load an image in the UIImageview. I hope some of you can help me out there.
below my code I thought might be correct but it isn't.
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 115)];
image.image = [UIImage imageNamed:[detailView objectForKey:#"thumbnail"]];
[self.view addSubview:image];
[image release];

i think in json u are getting url of image. display image from url using below code
//NSURL *url = [NSURL URLWithString:#"http://192.168.1.2x0/pic/LC.jpg"];
NSURL *url = [NSURL URLWithString:[detailView objectForKey:#"thumbnail"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 460.0f)];
[subview setImage:[UIImage imageWithData:data]];
[self.view addSubview:subview];
[subview release];

the code you posted
image.image = [UIImage imageNamed:[detailView objectForKey:#"thumbnail"]];
will try to find the image on your bundle named the string stored in [detailView objectForKey:#"thumbnail"]
As you mentioned, your images are from remote server, you have to download the image from your remote server.
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 115)];
image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[detailView objectForKey:#"thumbnail"]]]];
[self.view addSubview:image];
[image release]

Related

Unable to load an image into NSImageView

I wrote the following and no image comes up. I displayed the pointer and it comes out as "0".
In the next code block I checked the path and made shore the image was present there. Still nothing loads.
NSImageView *vi = [[NSImageView alloc] initWithFrame:CGRectMake(10, 10, 220, 220)];
[self.view addSubview:vi];
NSImage *dog = [NSImage imageNamed:#"airnow3.jpg"];
NSLog(#"pointer %d", dog);
[vi setImage:dog];
NSFileManager *filemgr;
NSString *currentpath;
filemgr = [[NSFileManager alloc] init];
currentpath = [filemgr currentDirectoryPath];
NSLog(#"%#", currentpath);
Why don't you use InitWithImage:(NSImage) instead of InitWithFrame
Maybe you should use UIImageView.
UIImageView *imageView = [[UIImageView alloc] initWithFrame: CGRectMake(10,10,220,220)];
UIImage *image = [UIImage imageNamed:#"airnow3.jpg"];
[imageView setImage: image];
[self.view addSubview: imageView];

Change UIImage imageOrientation metadata without affecting the image

I have an app which loads an image from the local disk and then displays it on a UIImageView. I want to set the image to aspect scale to fit.
The problem I'm having is that the imageOrientation is coming back as UIImageOrientationRight even though it's a portrait image and that's messing with how the aspect calculations are done.
I've tried a few methods of changing the meta data but both rotate the image when it gets displayed.
UIImageView *iv = [[UIImageView alloc] initWithFrame:self.frame.frame];
NSMutableString *path =[[NSMutableString alloc] initWithString: [[NSBundle mainBundle] resourcePath]];
[path appendString:#"/pic2.jpg"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *img = [UIImage imageWithData:data];
UIImage *fixed1 = [UIImage imageWithCGImage:[img CGImage]
scale:1.0
orientation: UIImageOrientationUp];
UIImage *sourceImage = img;
UIImage *fixed2 = [UIImage
imageWithCGImage:[img imageRotatedByDegrees:90].CGImage
scale:sourceImage.scale
orientation:UIImageOrientationUp];
iv.image = fixed1; // fixed2;
[iv setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:iv];
In the end I took a different approach and made the UIImageView 360 wide and placed it in the centre and this achieved the desired effect.

Create custom UIImage from json data

could you please provide me by sample code for creation a UIImage object with specific width and height programmatically in the code then load an Image in it which supposed to fetch from json (it has to be an image URL).
If I use synchronous network request for loading the picture does it block my UI till loading be completed or not? if so what's the solution?
Thanks in Advance
Pull out the image URL from your json data, download the image, and then use the image as needed.
NSString *imageURL = url from your json string;
NSURL *url = [NSURL URLWithString:imageURL];
UIImageView *imageView = [UIImageView alloc] initWithFrame:CGRectMake(0,0,50,75)];
[yourParentView addSubview: imageView];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
dispatch_sync(dispatch_get_main_queue(), ^{
imageView.image = image;
CGRect frame = CGRectMake(imageView.origin.x, imageView.origin.y, image.size.width, image.size.height);
imageView.frame = frame;
});
});

Unable to load image into UIImage

I have two images on Amazon S3 service. One loads fine and another does not. There isn't an error message. It just does not load.
[doodleImageView setImageWithURL:#"https://s3.amazonaws.com/doodlestash/doodles/20/thumb.jpg"];
//This one does not load.
[doodleImageView setImageWithURL:#"https://s3.amazonaws.com/doodlestash/doodles/20/original.jpg"];
Any thoughts?
The images do both appear to be "valid". You may want to double check the library you are using and check for typos. In a quick demo app I did the following:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView* image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
image1.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/doodlestash/doodles/20/original.jpg"]]];
UIImageView* image2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, 320, 200)];
image2.image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://s3.amazonaws.com/doodlestash/doodles/20/thumb.jpg"]]];
[self.view addSubview:image1];
[self.view addSubview:image2];
}
I ended up with this:

iOS: Add multiple UIImageViews programmatically in runtime

So, I'm doing a Breakout-clone on the iPhone. All elements except the bricks to hit, are created and working as expected with the NIB-file.
However, if I want to create different levels and run collision detection on the bricks, it seems stupid to add them in Interface Builder. How do I add them to the view in code?
I got an image called "brick.png" that I want to use with an UIImageView. Also, I want to have arrays and / or lists with these so I can build cool levels with pattern in the bricks and all :)
How do I do this in code?
#Mark is right, I would just add where the image need to be displayed!
UIImageView *imgView = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 20)] autorelease];
NSString *imgFilepath = [[NSBundle mainBundle] pathForResource:#"brick" ofType:#"png"];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgFilepath];
[imgView setImage:img];
[img release];
[self.view addSubview:imgView];
I tested the code and for me only shows when told the coordinates
It's really pretty easy. Here's an example of how you would create and display a UIImageView programatically...
UIImageView *imgView = [[[UIImageView alloc] init] autorelease];
NSString *imgFilepath = [[NSBundle mainBundle] pathForResource:#"brick" ofType:#"png"];
UIImage *img = [[UIImage alloc] initWithContentsOfFile:imgFilePath];
[imgView setImage:img];
[img release];
[self.view addSubview:imgView];
That's pretty much all there is to it.