How to stretch Image in OSX app? - objective-c

I want to stretch an Image to fit a view. In iOS I have used this function.
[myImage stretchableImageWithLeftCapWidth:12 topCapHeight:13];
This stretches the image correctly and doesn't distort its edges and corners. In OSX app development I am using NSImage and NSImageView. Is there any OSX equivalent way of achieving this? If it can be achieved through storyBoards then it will be wonderful.

You could simply set the imageScaling. Apples Documentation gives you the following options;
NSImageScaleProportionallyDown
NSImageScaleAxesIndependently
NSImageScaleNone
NSImageScaleProportionallyUpOrDown
If you just want to stretch the image to fit the NSImageView you can do something like this.
Swift
let imageView = ...
imageView.imageScaling = .ImageScaleProportionallyUpOrDown
Objective-C
NSImageView *imageView = ...;
[imageView setImageScaling: NSImageScaleProportionallyUpOrDown];

Related

outside border for UIImageView

I know I can create border using below code.
[[myImageView layer] setBorderWidth:2.0f];
[[myImageView layer] setBorderColor:[UIColor greenColor].CGColor];
However this draw border inside image.
What I was looking is draw border outside ImageView.
Note:
I search online for this and found below.
Can be done by using another image which will have border.
Can be done by drawing another view which is little bigger then current image.
Is there quick way (especially in-built in iOS), where I can draw border outside UIImageView? Any views?
Why don't you try border with using imageview's frame ?
CGFloat borderWidth = 5.0f;
self.imgview.frame = CGRectInset(self.imgview. frame, -borderWidth, -borderWidth);
self.imgview. layer.borderColor = [UIColor blueColor].CGColor;
self.imgview. layer.borderWidth = borderWidth;
There is no quickway in-built in iOS, there is no margin that you could set on the image layer.
If I were you, I'd develop a new class that inherit from UIView (ex UIImageWithBorderView) and which include a UIImageView and making the "UIImageWithBorderView" bigger than the UIImageView (and think about NOT to autoresize the UIImageView with the UIView parent, otherwise your UIImageView will be stretched, and prevent the UIImageWithBorderView from being smaller than the UIImageView frame), and then add borders to the "UIImageWithBorderView".
This way, your UIImageView will be intact and you'll have a specific, reusable composant for your needs.
Hope it helps !

Usage of retina images and setting an UIImageView

It is the first time that I need to use images with the name formats of like: #2x and -568h#2x But I do not understand when the -568#2x is used. This should be for iPhone 5 and up but, the image is not used on my iPhone 5S when testing.
I tried very hard searching for this problem, but I cannot find it.
Problem:
Only the #2x image is used on all devices, and the -568#2x is ignored totally. So there is a gap above my image on screen while I have set the UIImageView in Storyboard to fill the whole screen. So in my understanding, with the setImage it would look first in the Project folder, and then to the Asset Catalog. But still it does not take the -568#2x image and always take the #2x image.
I have also set the auto layout right for the UIImageView so it would fit top, bottom, left, right.
Code/Screen:
So I have 2 images in my Project folder as you can see on the image:
Link to image
Here is the gap on top:
Link to image
And then in my code I use this code for setting the image on my Outlet:
[[self image] setImage:[UIImage imageNamed:#"firstLaunch"]];
So what could be the problem? Am I totally wrong how the Automatic image names thingy is working?
Thanks!
You could do something like this:
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
self.view.layer.contents = (id)[UIImage imageNamed:#"firstLaunch#R4"].CGImage;
} else {
self.view.layer.contents = (id)[UIImage imageNamed:#"firstLaunch#2x"].CGImage;
}

set retina image in uiimageview

today i first run my app on my iphone but all my images (my background image too) were blurry, i read in the apple documentation and i understand that i should use a double pixel size (640X960 instead of 320X460). to set my image background i just use an uiiamgeview and set the image in the xib file. now, my question is how can i set the image from the .m file and not from my xib file. when i try to set the image from the .m file my image was much bigger then my uiimageview (i see only part from the image)?
thanks!
When using retina double sized images you don't actually have to do anything as far as loading them in code or in Interface Builder.
You just need to name the properly and make sure they're in your Xcode project.
For example if you had one image.png you'd need an image#2x.png in your Xcode project as well. iOS will automatically use the correct when when displaying on a Retina Device.
Update: In response to your comment.
This will create an UIImageView and set it to the size of the view. Its very similar to how most people do it in Interface Builder.
- (void)viewDidLoad
{
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"];
[background setFrame:CGRect(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
[self.view addSubview:background];
}

How to animate an image onto the screen

I am new to Objective-C and I want to let a screenshot made with
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
...
UIGraphicsBeginImageContext(imageSize);
...
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
"fly in" (rotating and stoping in random angle) inside an own image container, looking like a small photo (shadow, white border). how could I realize that?
Since you have a vague question - and your code shows only how you are getting the screen image - here's a general answer.
Put the image on a CAImageLayer
Add a border or a shadow or whatever other chrome you need to your image layer (or on another layer underneath it)
Use Core Animation to animate this CAImageLayer to whatever position or state you want it to be in.
You can accomplish it with OpenGL ES.
Here is a great project which provides generic base OpenGL ES view transition class.
https://github.com/epatel/EPGLTransitionView
There are several predefined transitions, you can take as an example https://github.com/epatel/EPGLTransitionView/tree/master/src
Demo3Transition actually shows how to rotate the screenshot
https://github.com/epatel/EPGLTransitionView/blob/master/src/Demo3Transition.m
You can view available transitions in actions if you launch DemoProject.

How do you position a larger NSImage inside of a smaller NSImageView programmatically?

Let's say I have an NSImage that's 100x100. I also have an NSImageView that's 50x50. Is there a way I can place the NSImage at coordinates inside the NSImageView, so I can control which part of it shows? It didn't seem like NSImage had an initWithFrame method...
I did this in my NSImageView subclass, as Andrew suggested.
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
NSRect cropRect = NSMakeRect(x, y, w, h);
[image drawAtPoint:NSZeroPoint
fromRect:cropRect
operation:NSCompositeCopy
fraction:1];
}
I don't believe so, but it's trivial to roll your own NSImageView equivalent that supports center/stretch options by drawing the image yourself.
Make your imageview as big as the image, and put it inside a scrollview. Hide the scrollers if you want. No need for subclassing in this case.
NSImageView has a method -setImageAlignment: which lets you control how the image is aligned within the image view. Unfortunately, if you want to display part of the image that doesn't correspond to any of the NSImageAlignment values, you're going to have to draw the image programmatically.
Depends on what your eventual goal is but the easiest thing to me seems to put your NSImageView inside an NSView (or a subclass – doesn't have to be NSScrollView as "#NSResponder" user suggests but this should work well too), set its imageScaling to NSImageScaleProportionallyUpOrDown and its frameSize to image's size. Then you can move your NSImageView freely around the upper view using setFrame:myDesiredFrame. No subclassing, no manual redrawing, etc.