Rectangle 27 9

-(CGRect) cropRectForFrame:(CGRect)frame
{
    NSAssert(self.contentMode == UIViewContentModeScaleAspectFit, @"content mode must be aspect fit");

    CGFloat widthScale = self.bounds.size.width / self.image.size.width;
    CGFloat heightScale = self.bounds.size.height / self.image.size.height;

    float x, y, w, h, offset;
    if (widthScale<heightScale) {
        offset = (self.bounds.size.height - (self.image.size.height*widthScale))/2;
        x = frame.origin.x / widthScale;
        y = (frame.origin.y-offset) / widthScale;
        w = frame.size.width / widthScale;
        h = frame.size.height / widthScale;
    } else {
        offset = (self.bounds.size.width - (self.image.size.width*heightScale))/2;
        x = (frame.origin.x-offset) / heightScale;
        y = frame.origin.y / heightScale;
        w = frame.size.width / heightScale;
        h = frame.size.height / heightScale;
    }
    return CGRectMake(x, y, w, h);
}

You can pass in the frame of your green rect (assuming it is a subview of the image view) and get the crop rect back for cropping the UIImage. Note that it only works for 'aspect fit' mode! I haven't tested it though. So, tell me if it works!

Haven't gotten a chance to implement this yet, but I will ASAP. Thanks for this, seems like it makes sense.

Can you help me with the above code for scale to fill mode ?

How would you adjust this algorithm to account for images that are 2x and 3x? For 1x images this works perfectly but does not crop accurately for images with 2x and 3x scale.

ios - How to crop a UIImageView to a new UIImage in 'aspect fit' mode?...

ios uiimageview uiimage
Rectangle 27 3

I think you should set the content mode of gifView to Aspect Fit, doing so will not stretch the image and will fully fill at-least one of the length either horizontally/ vertically.

scale
AspectFill
gifView

ScaleAspectFill will fill the entire gifView's frame but will also maintain the aspect ratio, doing so your image won't look stretched but it could happen that content either horizontally or vertically will go outside the frame(which you can clip).

gifView.contentMode = UIViewContentMode.ScaleAspectFil

ios - How to fill the background image in a UIView without streching i...

ios swift uiview
Rectangle 27 1

In interface Builder, access the Mode menu inside of the detail pane (the fourth one) and choose the right one for your UIImageView (I guess "center" or "aspect fit").

You can use the contentGravity property of CALayer to make it work

A constant that specifies how the layer's contents are positioned or scaled within its bounds.

@property(copy) NSString *contentsGravity

Didn't work using "Center"... The image fit all the view, beyond the frame image. Do you have an example of how to use the contentsGravity?

what about aspect fit?

This keeps the image aspect but I get some blank areas in the top and bottom of the frame image.

It doesn't work. When I posted this, it was with aspect fill. Is there any parameter I need to set in IB, beyond Aspect Fill?

iphone - Crop UIImage to fit a frame image - Stack Overflow

iphone ios objective-c uiimage
Rectangle 27 2

Depending on the graphics in some cases it might work fine when we use just a single image for example a banner with size 414 points width x 100 points height (largest possible width and some fixed height) and put it in a UIImageView that is pinned to the left and right edge of the screen, has fixed height 100 and set aspect fill mode for that UIImageView. Then on smaller devices left and right side of the image will be cut and we will see only the center part of the image.

ios - How to handle image scale on all the available iPhone resolution...

ios iphone screen-resolution iphone-6 iphone-6-plus
Rectangle 27 1

First make sure you have selected the correct size class. The 'Compact Width | Regular Height' size class must be selected in the Interface Builder. Now add the Trailing space,Leading Space, Top space and Bottom space constraints to the table view. For the image view set the view mode to Aspect fit and add the constraints : Align Center Y ,Top space,Bottom space, Leading space, Trailing space and Aspect Ratio .

You can still use the same constraints even if you are'nt using size classes.

And use a UITableViewController instead of placing a UITableView in a UIViewController if you dont plan on having static table view cells. You cannot have dynamic table view cells if the table view is not in a UITableViewController. You could also use a container view inside a UIViewController and set a UITableViewController as its root view.

I can't use UITableViewController because in this case I am unable to place UIImageView at the bottom of the view controller. Am I wrong?

You can definitely add a UIImageView to the end of a UITableView in a UITableViewController. But this image will be placed after the last row in the table which wont be visible if there are many rows and if you need to scroll. If you want the image to always be at the bottom of the screen irrespective of the number of rows then use a UIContainerView for the table view.

ios - How to correctly use constraints when both UITableView and UIIma...

ios xcode user-interface interface-builder xcode6
Rectangle 27 0

The code below sets the mode for your image view to aspect ratio and to fill area of screen.

//Makes your image view fill the real estate you have available on your phones screen
yourUIImageView.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
//Will make your image view Aspect ratio
yourUIImageView.contentMode = UIViewContentMode.ScaleAspectFit

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

ios - AVMakeRectWithAspectRatioInsideRect returning correct image but ...

ios iphone swift uiscrollview uiimageview
Rectangle 27 0

It depends on the image view content mode.

CGPoint pointOnImage = CGPointMake(pointOfTouch.x*(imageSize.width/frameSize.width), pointOfTouch.y*(imageSize.height/frameSize.height));

For all other modes you need to compute the actual image frame inside the view which have different procedures then.

Adding aspect fit mode from comments:

For aspect fit you need to compute the actual image frame which can be smaller then the image view frame in one of the dimensions and is placed in center:

CGSize imageSize; // the original image size
    CGSize imageViewSize; // the image view size

    CGFloat imageRatio = imageSize.width/imageSize.height;
    CGFloat viewRatio = imageViewSize.width/imageViewSize.height;

    CGRect imageFrame = CGRectMake(.0f, .0f, imageViewSize.width, imageViewSize.height);

    if(imageRatio > viewRatio) {
        // image has room on top and bottom but fits perfectly on left and right
        CGSize displayedImageSize = CGSizeMake(imageViewSize.width, imageViewSize.width / imageRatio);
        imageFrame = CGRectMake(.0f, (imageViewSize.height-displayedImageSize.height)*.5f, displayedImageSize.width, displayedImageSize.height);
    }
    else if(imageRatio < viewRatio) {
        // image has room on left and right but fits perfectly on top and bottom
        CGSize displayedImageSize = CGSizeMake(imageViewSize.height * imageRatio, imageViewSize.height);
        imageFrame = CGRectMake((imageViewSize.width-displayedImageSize.width)*.5f, .0f, displayedImageSize.width, displayedImageSize.height);
    }

    // transform the coordinate
    CGPoint locationInImageView; // received from touch

    CGPoint locationOnImage = CGPointMake(locationInImageView.x, locationInImageView.y); // copy the original point
    locationOnImage = CGPointMake(locationOnImage.x - imageFrame.origin.x, locationOnImage.y - imageFrame.origin.y); // translate to fix the origin
    locationOnImage = CGPointMake(locationOnImage.x/imageFrame.size.width, locationOnImage.y/imageFrame.size.height); // transform to relative coordinates
    locationOnImage = CGPointMake(locationOnImage.x*imageSize.width, locationOnImage.y*imageSize.height); // scale to original image coordinates

Just a note if you want to ransfer to aspect fill all you need to do is swap < and > in both of the if statements.

Try the code I added. I did not try it so I hope it will just work, otherwise get back to me. Also try editing your comment as you have written "fir".

ios - Get the pixel position of pan On UIImage in a UIImageView - Stac...

ios uiimage scaling
Rectangle 27 0

With content mode set to Aspect Fill, try setting clips to bounds to true as well, reason being the content mode aspect fill keeps on filling the frame of the image view till the frame is fully filled with content also keeping the aspect ratio intact. In the process of filling the container with image maintaining aspect ratio, either vertical or horizontal frame is fully filled, and the filling is continued till the other (if horizontal than vertical or vise versa) portion is fully filled. Thus the first filled portion either across vertical or horizontal will go out of bounds and the content will be visible outside the frame of the image view. To clip the extra content we need to clip the extra portion using imageView's clipsToBounds property set to true

cell.photo.contentMode = UIViewContentMode.ScaleAspectFill
cell.photo.clipsToBounds = true

ios - Why scale to fill give bigger image than UIImageVIew size? (usin...

ios swift parse.com pfimageview
Rectangle 27 0

Depending on the graphics in some cases it might work fine when we use just a single image for example a banner with size 414 points width x 100 points height (largest possible width and some fixed height) and put it in a UIImageView that is pinned to the left and right edge of the screen, has fixed height 100 and set aspect fill mode for that UIImageView. Then on smaller devices left and right side of the image will be cut and we will see only the center part of the image.

ios - How to handle image scale on all the available iPhone resolution...

ios iphone screen-resolution iphone-6 iphone-6-plus
Rectangle 27 0

In iOS 4.3 the overlay view is stretched to full screen. Because you set the content mode to aspect fit, the image is stretched to fit the new view size which is 320x480.

You need to make a transparent UIView that is fullscreen, add the imageview to that view and make the UIView the new overlay view.

UIView *fullscreenView = [[UIView alloc] initWithFrame:CGRectZero];
fullscreenView.backgroundColor = [UIColor clearColor];
....
 [fullscreenView addSubview:imgView];
pickerController.cameraOverlayView = fullscreenView;

iphone - cameraOverlayView problem on iOS 4.3 - Stack Overflow

iphone uiimageview uiimagepickercontroller ios4
Rectangle 27 0

Try resizing your images to the frame size of your UIButton and generate an @2x copy for your retina display. You can also try setting the mode to aspect fit in the attribute inspector.

iphone - I added UIButtons to my app and made custom images for them, ...

iphone ios objective-c xcode uibutton
Rectangle 27 0

But that's a problem, isn't it? It means: "Stretch the image so that it matches the way the image view is stretched." If that isn't what you want, don't say that! Use Centered, or at least use one of the content modes with "Aspect" in its name so that your image is not stretched.

As for the circle itself, setting the cornerRadius is no way to make a circle. The way to create a circular boundary around an image is to mask the image. You can redraw the image with the circular mask as a clipping boundary, or you can apply the circular mask to the image view. (See, for example, my answer here: http://stackoverflow.com/a/16475824/341994.)

It is true that your image view is also being stretched, because you gave it constraints to both sides of the superview. You can prevent that by giving it a width constraint instead; now its width will be absolute. But you should still do the right thing on the other two issues.

Can you expand on your comment that setting the corner radius is no way to make a circle? I see many people using that technique; it obviously works, so what's wrong with it?

@rdelmar You see many people using it, but if you will look you will see just as many people complaining that it has flaws - as in this very question. It's just lazy. If you want rounded corners, and you don't care about accuracy (flat edges) or stretching (as here), fine, round the corners. But if you want a circle, ask for a circle. Common sense, really.

ios - Storyboard and autolayout: how make a circular image - Stack Ove...

ios objective-c xcode storyboard autolayout
Rectangle 27 0

Yes, resizing the image prior to the assignment to its ImageView will reduce the jaggies that you see. To resize a UIImage you can check this answer

Thank you for your answer. I was looking for a good way to resize the image prior to displaying it, and your link did the trick. For those who want to maintain the "Aspect Fit" content mode, this link should provide with what you need: iphonedevsdk.com/forum/.

iphone - UIImageView: Content Mode Aspect Fit Distorts Image - Stack O...

iphone objective-c cocoa-touch uiimageview uiimage
Rectangle 27 0

Yes, you can theoretically use the largest resolution image and have the UIImageView scale the image down using mode Aspect Fit.

The only drawback is the older phones that don't support retina also are less performant. For example, when using images on cells of a UITableView and scrolling, the device has to load the large image, then scale the image down, and scroll it at the same time, and it will stutter on old, slower devices.

So, perhaps just use multiple images in list views (should just be thumbnails and are tiny anyway, or just use the smaller images here), but don't worry about larger images that stay on the screen and don't scroll.

Make sure you load images in list views using methods that allow caching like imageNamed:.

As long as you take into consideration the performance penalties involved in scaling down the images, you can use just the largest image and scale it down to fit.

By the way, yes I've used this technique in real live apps in the App Store.

Another technique I've seen is to include lower quality images (1x or 2x) and if you run it on a higher resolution device (2x or 3x), automatically download high resolution images from the web. Maybe be nice about it and only download them on wifi.

Thank you for answering, I am supporting the only non retina devices I am supporting form iOS 7 upward is the iPad 2 and iPad mini 1. And I am loading the images as you suggest with imageNamed: Thank you for confirming that the technique works.

Also, Apple upped the maximum app size that you can download over cellular. It's now 100MB (was 50MB).

Much easier just to upload the highest res image and be done with the 2x and 3x business. If you use minimum size 2208x2208, it should cover all devices. Run your images through some compression software and you'll reduce size. Here's some info: paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions

ios - Using less Images for non-retina, retina and retina HD (@1x, @2x...

ios xcode uiimageview universal
Rectangle 27 0

There are a couple of ways you can achieve this. The simplest is to set the scaling mode on the image view to Aspect Fill in Interface Builder, or

imageView.contentMode = UIViewContentModeScaleAspectFit;

The second way is to calculate the aspect ratio of the image and set the width. Something like:

UIImage *myImage = [UIImage imageNamed:@"MyImage"];
float aspectRatio = myImage.size.width / myImage.size.height;
float viewWidth = 100 * aspectRatio;

iphone - how to set UIimageview width with respect to the original ima...

iphone objective-c uiimageview uiimage
Rectangle 27 0

There are a couple of ways you can achieve this. The simplest is to set the scaling mode on the image view to Aspect Fill in Interface Builder, or

imageView.contentMode = UIViewContentModeScaleAspectFit;

The second way is to calculate the aspect ratio of the image and set the width. Something like:

UIImage *myImage = [UIImage imageNamed:@"MyImage"];
float aspectRatio = myImage.size.width / myImage.size.height;
float viewWidth = 100 * aspectRatio;

iphone - how to set UIimageview width with respect to the original ima...

iphone objective-c uiimageview uiimage
Rectangle 27 0

I would suggest not to give your images height, instead add a vertical stackview into your container view and add all your images in there. make sure they have content mode set to aspect fit and the stackview should take care of the rest. don't forget to constrain the stackview to the bottom of the label and the bottom of the cell, so it knows how much space it has. it will take care of your images by itself

I tried, but Images are overlapping and constrains are breaking.

@Florens von Buchwaldt this looks more like a comment .You should give suggestions in comment not in answer.

@Sneak, UIStackView is working well without breaking constraints. But now I am facing new issue. Cells have different count of image views and when I dequeue reusable cells of table, stack views already includes some image views. How can I handle this? I tried remove all image views and added, but it is not working well.

ios - Dynamic Height Table Cell with Autolayout and UITableAutomaticDi...

ios objective-c iphone uitableview autolayout
Rectangle 27 0

UIImageView respects contentMode which would likely serve your purpose here easier than a mask. Set your imageView to the fixed size you want all images to fit in. If you want the image to squeeze in and show black bars if its too tall for example, use UIViewContentMode.scaleAspectFit. but more likely what will look best is UIViewContentMode.scaleAspectFill. It will maintain the aspect ratio of your image and scale until it fills the imageView box you've created. Play with the two modes and others to see their effects.

let imageView = UIImageView(image: UIImage(named: "myImage"))
imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imageView.contentMode = .scaleAspectFill

More info on various contentModes you can try here.

ios - Masking a UIImageView doesn't work - Stack Overflow

ios swift uiview uiimageview mask
Rectangle 27 0

When you have two versions of the image (normal and @2x), use the "normal" name (without @2x) in XIB or UIImage#imageWithName:, system will automatically choose the best version for the current screen. Also check that your UIImageView size corresponds to the normal (not 2x) resolution of your image. There are several content modes (like Aspect Fit, Aspect Fill, Center, etc.) that will resize or position your image in the UIImageView.

so is it necessary to put two images (normal and @2x) to the project, when set it as the image of a button?

Yes, if you want to support both retina and non-retina displays (that is, if you want to support devices older than iPhone 4).

Is it ok to provide the high resolution image for the non-regina displays?

I don't have two versions of the image, what I am trying to do is, for example, taking a 200x200 image and putting it into a 100x100 frame so it will work in both normal and retina. This seems to work for alot of other images in my program, but Xcode is simply not recognizing the size change when I do this....

iphone - Xcode does not recognize change in size of image - Stack Over...

iphone objective-c ios image retina-display
Rectangle 27 0

I think that it doesn't ignore it.Your setup is correct. You just have to stop drawing outside image view. Check this on ImageView: Clip Subviews ->enabled. Choose also good View fill mode: Scale To Fill or Aspect Fit.

Thanks for the suggestion, I had seen the clip subviews in another post but it doesn't fix it. 'Scale to fit' is also selected by default, I tried 'Aspect fit/fill' but they don't seem to help.

ios - UIImageView Ignores AutoLayout Constraints in UITableViewCell - ...

ios uiimageview autolayout