How can I trim a UIImageView to fit an aspect ratio image - objective-c

I am using a crop tool in my app and I need to modify a UIImageView so that it fits an image exactly after inserting the image in aspect fit mode.
So an image is selected and added to the UIImageView in aspect fit mode. The problem is that this then leaves "blank space" around the image inside the UIImageView that needs trimming. I was wondering how I could then go and resize the holding UIImageView based upon the image inside.
Is this possible?

The Easy way is simply using the following code on your "imageView"
imageView.contentMode = UIViewContentModeScaleAspectFit;
Assuming you want to cut a "zoomed" section of your image to fit fully into your imageView
check your original image width and height
Assuming width is bigger in size then height , scale the image width to the holder width
center the image on your holder , the width will fit perfectly (section2) and the height will simply be cropped follow above and below the holder.

It turns out that a better approach is to use the following idea.
How to get the size of a scaled UIImage in UIImageView?
Instead of trimming the UIImageView, insert the image and then get the dimensions of the image inside the UIImageView, from there you can then resize the UIImageView to match the dimensions of the image inside.

Related

Appcelerator Studio - Don't stretch Image

I have a 405x720 image that it is to cover the background image. I've noticed that it stretches the image if the device's screen is smaller than the image. (I'm using a Galaxy S4 to test my apps).
Here's what I have so far:
var fullView = Ti.UI.createImageView({
layout: 'vertical',
width: '100%',
height: '100%',
image: '/images/background.png'
});
I've heard about the clipping technique, however that is only available for iOS and I'd like to have it for both platforms. Is there a way to do this? Or should I try to resize it appropriately? Tips/Suggestions are appreciated!
You should use proper images according to the aspect ratio of screen you are going to run your app on.
You can do this by calculating the width-height ratio of device and then you can decide which image to show on.
But putting a single resolution image directly to cover up the whole screen
is never going to work.
To resize an image, you can use Ti.Blob class' methods after fetching your image using Ti.Filesystem
Also remember that putting an image on whole background takes lot of memory especially in Android. That's why most of the apps do not use complete image to cover up background, rather they use small images wherever necessary and fills up other areas by colors or gradients as required.
But if your requirement is only to use image in background then unload it as soon as you move to different screen and load it again as you are back on it.
Further points to keep note of:
If you set bg image on a View, then it will be fit to cover the dimension of a View. So, if you have a view of width/height to 100%, then image will be fit into it. It means that image size will depend on View's dimension, not the View dimension will depend on image size.
If you set image on ImageView, and suppose you have:
ImageView width = FILL to screen size of 720, then the image height will be automatically decided to keep the aspect ratio. It means that if you have an image of size width=720 and height=400, then the ImageView will be of width=FILL & height=400
Image of size width=1000 and height=800 & ImageView height = 400, then the ImageView will be of width=500 & height=400
The problem is that you're actually telling the image control to load the image on an object that is stretched to the entire width and height of the screen - and when the screen aspect ratio is different than the one you have cropped your image by then you get bad aspect ratio and image looks screwed up.
If you remove either the width or the height, than the aspect ratio of the image will be loaded.
When I want a background image what I found the best way is to make a square image, and then set my ImageView height to Ti.UI.FILL and my width to Ti.UI.SIZE - That way it shows good aspect ratio and side that dont "fit" the screen (width or height) are not shown.

NSImageView won't scale image down if frame style is NSImageFrameNone

I need to display NSImageView on resizable NSWindow. The image displayed in the view should scale down if it's too big to fit image view in the widnow and it should also change its size when resizing the window. I am able to achieve correct behavior using Auto-Layout, and setting imageScaling property of NSImageView to NSImageScaleProportionallyDown. Unfortunately it's not working when my image view's imageFrameStyle property is set to NSImageFrameNone (it works for any other option like NSImageFramePhoto or NSImageFrameGrayBezel). I don't want any frame to be displayed, like with NSImageFrameNone, but disabling the frame breaks autoresizing - it looks like with frame set to NSImageFrameNone image is not scaled down and NSImageView is scaled up to match displayed image size. Anyone have solution for that issue?
It occurs that it's easier than I thought. The problem was with Auto-Layout, not with NSImageView scaling logic. Using InterfaceBuilder with my image view selected I had to change "Content Compression Resistance Priority" to lower value (it's located on Size Inspector tab in Xcode5). That solved my problem and the image is now scaled properly when resizing window.

UIImageView with another UIImage of frame

i m developing an photo based app
i have two imageview one imageview contains the original image
and other imageview contains the frame
Now the problem is i want to manage the frame Imageview according to the main image
i.e. if my main image view is of size 320x480 then border applies on that size
and if my image view is of size 200x150 then border have to applies on that size
How to do that
UIImage.size can give you dimensions of image you are using. you can resize your UIImageView.frame using this size. this will fit UIImage in UIImageView without any extra space so borders will appear perfect.
second option could be to initialize an image view once again using following method.
- (id)initWithImage:(UIImage *)image
this will make a perfect sized imageview for your image.
Let me know if you are not looking for something like this....

Aspect ratio in UIImageView

I got app which can add photo from camera or gallery. After adding each photo showes in UIImageView. But they show with bad aspect ratio. Photoes are extended on UIImageView's size.
How can i show photoes with their real aspect ration?
UPD and what about small pictures? if user will add really small picture, this picture will resize on all area of UIImageView. How show this pictures in real size?
Doing myImageView.contentMode = UIViewContentModeScaleAspectFit; is one option.
But it isn't enough because it changes the aspect ratio of the image, but it doesn't change the source image.
The only way to do that is to use the UIGraphicsImageContext as explained there:
Resize UIImage with aspect ratio?

Change UIImageView height while scaling down to specific width

I have a UIImageView inside a UIScrollView.
What I am trying to achieve is to have a flexible height for the UIIMageView depending on the image that is loaded in while keeping the width of the UIImageView the same.
I also don't want the UIImageView to scale from the centre, but rather from the top most point.
Any help is greatly appreciated.
Thanks,
Ashey
First of all, adjusting the height of a UIView (or any of its subclasses) will adjust the height from the origin.y coordinate downwards. It doesn't span from the center.
Do you know the sizes of your image beforehand? If the images appear in the same order each time, you can just supply the sizes in an array.
If you do not know the sizes, or the order of the images, you can use the size property on the UIImage you are putting in your UIImageView. This will return to you a CGSize, of which you can take the height. You will also need to update the contentSize of the UIScrollView.