Swift equivalent of initWithContentsOfFile using image - objective-c

In Objective C I was using this code to scale an image to my background. How can I achieve this in Swift.
path = [[NSBundle mainBundle] pathForResource:#"This" ofType:#"png"];
image = [[UIImage alloc] initWithContentsOfFile:path];

you can try this:
path = NSBundle.mainBundle().pathForResource("This", ofType: "png")
image = UIImage(contentsOfFile: path)

Related

swift to objective C set image prom facebook login

How can I write the code below in Objective C:
let strPictureURL: String = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!
self.ivUserProfileImage.image = UIImage(data: NSData(contentsOfURL: NSURL(string: strPictureURL)!)!)
I have been trying to rewrite the code in objective C and don't find a way to set an image from a NSString. So far I have managed this:
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
self.ivUserProfileImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strPictureUrl]]];
I managed to make it work like this:
NSString *strPictureURL = [[[result objectForKey:#"picture"] objectForKey: #"data"] objectForKey:#"url"];
UIImage *im = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:strPictureURL]]];
[self.imageProfilePic setImage:im];

I cannot create NSImage with imageNamed but initWithContentsOfFile

With the following code:
NSString *imageString = [[NSBundle mainBundle] pathForResource:#"logo64x64" ofType:#"png"];
NSImage *testImage = [[NSImage imageNamed:#"logo64X64"] retain];
NSImage *testImage2 = [[NSImage alloc] initWithContentsOfFile:imageString];
testImage is nil but testImage2 is a NSImage instance. I don't know what's wrong with the code. I'm sure I can find logo64x64.png and logo64x64#2x.png in the resource directory in the bundle. I've also tried imageNamed:#"logo64X64.png" but still getting nil.
Anyone can help?
Try
[NSImage imageNamed:#"logo64x64"];
rather than
[NSImage imageNamed:#"logo64X64"];
Note that in the code that succeeded, you used a lowercase x, whereas in the code that failed, you used an uppercase X (logo64X64). NSBundle is case-sensitive, even if the underlying HFS+ file system is only case-preserving. (NSImage's +imageNamed: method uses NSBundle to locate resources).

UIImage initWithContentsOfFile doesn't work

I have question: I want to avoid [UIImage imageNamed:].
So i did:
UIImage *prodImg = [[UIImage alloc] initWithContentsOfFile:#"myimage.png"];
controller.productImg.image = prodImg;
[prodImg release];
But now the image is not shown anymore.
Does anyone know how to get that to work?
The above code is not working because correct way to do it is-
NSString *thePath = [[NSBundle mainBundle] pathForResource:#"default" ofType:#"jpeg"];
UIImage *prodImg = [UIImage imageWithContentsOfFile:thePath];
controller.productImg.image = prodImg;
[prodImg release];
;)

Thumbnail from NSURL

I am loading files that are downloaded from the web in a UIWebView (HTML files).
Is there a way to create a UIImage from an HTML file located in the Documents folder?
The goal is to auto generate thumbnails without needing to manual create them...
Update
My train of thought would be to do something in the line of:
NSString * s = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
UIImage *image = [[UIImage alloc] initWithData:
[NSData dataWithContentsOfURL:url]];
Which of course does not work in this case!
Thanks!
S.
I found this solution here
UIGraphicsBeginImageContext(yourWebView.bounds.size);
[yourWebView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultImageView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
resultImageView will be an image that contains the render of yourWebView.
If you want to shrink it down to a thumbnail size from there, I like Trevor's UIImage category for that purpose.

iPhone SDK and Multipage tiff

I need to access the iphone APIs to read a multipage TIFF image file format, dose anyone know how:, if not how can I build and use the open source libtiff on iphone/xcode.
I've created my own solution for this: NSTiffSplitter.
You can find it on github.
NSString *pathToImage = [[NSBundle mainBundle] pathForResource:#"Example" ofType:#"tiff"];
NSTiffSplitter* splitter = [[NSTiffSplitter alloc] initWithPathToImage:pathToImage];
UIImage *page = [[UIImage alloc] initWithData:[splitter dataForImage:page]];
yourImageView.image = page;
[page release];