What is the equivalent of UIImage imageWithData for NSImage - objective-c

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

Related

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

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"]]])];

Upload pictures to my app

I´m designing a new app for an iPad. This app will work with a barcode scanner, and when it scans a code, it will show an image asociated.
I was thinking to build that asociating de barcode to an image name for example:
- Barcode 09090909 will show 09090909.png picture
- Barcode 19191919 will show 19191919.png picture
....
I think that there is no problem with that, but the problem comes when I need to add new barcode/pictures to the app. How can I send a new picture to my App? I see that when you develop on XCode and build you App, all the data goes into de App.
Any help or clue? thanks in advance
You can simple load them on the fly from your server, but then a network connection is required. To load them async from an server you can use this code:
dispatch_queue_t image_queue = dispatch_queue_create("com.company.app.imageQueue", NULL);
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(image_queue, ^{
NSString *URLString = #"http://example.com/images/19191919.png";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(main_queue, ^{
[imageView setImage:image];
});
});
If you want you can also save the image to the cache dir (using the document dir is not recommended because it will collide with Apples iOS Data Storage Guidelines) and check before downloading an image twice, then the code will look like this:
NSString *fileName = #"19191919.png";
NSString *URLBaseString = #"http://example.com/images/";
NSString *URLString = [URLBaseString stringByAppendingString:fileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
NSString *dataPath = [cachePath stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( [fileManager fileExistsAtPath:dataPath] )
{
UIImage *image = [UIImage imageWithContentsOfFile:dataPath];
[imageView setImage:image];
}
else
{
dispatch_async(image_queue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString];
UIImage *image = [UIImage imageWithData:imageData];
[UIImagePNGRepresentation(image) writeToFile:dataPath atomically:YES];
dispatch_async(main_queue, ^{
[imageView setImage:image];
});
});
}
This code has not been tested, but should work. Note that I'm relying on ARC to manage memory.

Load image from plist

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

Objective C, Can't load an image from data returned by ASIHTTP request

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSData *data = [NSData dataWithData:[request responseData]];
NSData *responseData = [request responseData];
UIImage *img = [[UIImage alloc ]imageWithData:data];//it blows up here
self.q.image = img;
[img release];
self.request = nil;
[delegate imageDidLoad:self.indexPathInTableView];
}
I am downloading an image data using ASIHTTP request and it worked fine. But If I try to create an image using the data from ASIHTTP request then it blows up..What is the problem?
Thanks in advance...
Its because you are using imageWithData: to create your image, you should use initWithData: or just [UIImage imageWithData:data] instead of alloc'ing first.
Your code doesn't work because imageWithData: is a static method on UIImage that generates an autoreleased UIImage instance. Because it is a static method, not an instance method, it isn't a recognised selector on the UIImage instance you get when you call [UIImage alloc]
as the error says the it's an unrecognized selector.
you should call - (id)initWithData:(NSData *)data.
[[UIImage alloc] initWithData:data];
or
[UIImage imageWithData:data];