Load image from plist - objective-c

I am trying to input the link of the image on one of the plist <string>.
What code should I add in to the implementation in order to load the image from that website and display in an UIImage?

This should work:
NSData *imgData = [[NSData alloc] initWithContentsOfURL:urlFromPlist];
UIImage *img = [UIImage imageWithData:imgData];
[img drawInRect:someRect];

Related

What is the equivalent of UIImage imageWithData for NSImage

I want to load an image into my MAC OS desktop application. I have found plenty of documentation on how to do this on mobile iOS but how do I load the image on my desktop app since there is no UIImage(imageWithData).
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:#"myurl"]];
productImage.image = [NSImage imageWithData:imageData];
imageWithData: is a convenience initializer for initWithData: in UIKIt. AppKit doesn't seem to have this, so you'll just need to use initWithData: like this:
productImage.image = [[NSImage alloc] initWithData:imageData];
See this page for more info: https://developer.apple.com/reference/appkit/nsimage/1519941-initwithdata?language=objc

Adjusting UIImageView size and location based on the size of image fetched from service

In my iOS7 app I have view just like profile page in any social networking site. I have a profile image in top left corner. I am fetching the image from service url and loading it to a UIImageview.
My problem is, I have to increase the size of this image view when the size of the image from service is big. And also i have to adjust other UILabels beside this image view. I am using this code to get the size of image from url:
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
CGSize size = img.size;
I don't know initWithData:cache: method on UIImage class, is this category thing?
Anyway you can make UIImageView dimension to fit content UIImage by [imageView sizeToFit].
If you want to adjust scale, use [UIImage imageWithData:data scale:scale].
NSString *path = #"http://www.example.com/example#x2.png"
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data scale:2.0];
_imageView.image = img;
[_imageView sizeToFit];

Tableview Images Loading Slowly

I am trying to load image from a JSON feed into a tableview. The code below returns the images but it is making the app very very slow and "sticky". Any ideas on how to make it faster? This is being called for each cell in the tableview.
NSURL *imageURL = [NSURL URLWithString:#"http://www.site.com/images/image_1.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *cellImage = [UIImage imageWithData:imageData];
Thank you all!
Please try the below code.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSURL *imageURL = [NSURL URLWithString:#"http://www.site.com/images/image_1.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *cellImage = [UIImage imageWithData:imageData];
dispatch_sync(group, ^{
cell.image = cellImage;
});
});
dispatch_release(group);
Loading of images from the Internet in main thread is always bad idea.
You need to subclass UITableViewCell and do loading of images in background thread.
Here you have few suggestions how to do it.
Lazy load images in UITableView
Load image to a tableView from URL iphone sdk

Extract image from URL

The url is:
https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com
I want to get above image into to a UIImage. I tried this:
NSURL *urlString = [NSURL URLWithString:#"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com"];
NSData *imageData = [[NSData alloc]initWithContentsOfURL:urlString];
UIImage *img = [UIImage imageWithData:imageData];
But it seems this doesn't work. The url doesn't have an file extension like .png or .jpg. Is is possible to extract the image from this kind of URL?
You were close, what you're going to want to do is UIImagePNGRepresentation in the case of this particular image (because the image you linked is actually .png) to basically convert the data to a workable image. If you are trying to use a .jpg image use UIImageJPEGRepresentation.
NSURL *urlString = [NSURL URLWithString:#"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:urlString]])];
UIImage *image = [UIImage imageWithData:imageData];
Or, if you're as big of a fan on one-liners as I am, you can use this:
UIImage *image = [UIImage imageWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://api.url2png.com/v3/P4DE5D1C99D8EF/7bbb6e0d1b74fb0ae1d4f18b06320096/400x400/abc.com"]]])];

how to convert nsdata to image?

I am developing an application in which I take NSData and convert it into image an display it in the image view. Now i want to save this data and display it again in UIImageView.
I used NSUserDefaults to save the NSData and again retrieve this data and convert into image and display it in image view.But i am not getting the image back.my code is as follows:
//for saving nsdata using nsuserdefaults method:-
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];
[defsMVC setObject:stateMVC forKey:#"MapImageObject"];
//for retrieving nsdata from nsuserdefaults and display it in imageview
NSUserDefaults *defsMVC=[NSUserDefaults standardUserDefaults];
NSData *stringmapdata=[defsMVC dataForKey:#"MapImageObject"];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 320,415)];
UIImage *image = [UIImage imageWithData:stringmapdata];
[imageView setImage:image];
[image release];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
[imageView release];
I don't think you should be storing the data in the user Defaults, try storing it in a file like "yourimage.png", then you can simply do [[NSImage alloc] initWithConentsOfFile:path];
Also try using NSArchiver before saving it in the user defaults:
NSImage *image = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:#"someKey"]];
And for archiving it to the user defaults:
[[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:image] forKey:#"someKey"];
Gotta love Cocoa =)
My image is stored in 6th column in a form of NSData, first I have to get that data into NSData object. Than convert that NSData into image.
data = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 6)
length:sqlite3_column_bytes(statement, 6)];
img = [UIImage imageWithData:data];
imgdata = [[NSMutableArray alloc]initWithObjects:img, nil];