iOS Swift: Match UIImage frame size with Image pixel size - ios

I have a table view custom cell in which I am uploading images from Parse database. I want to display the Full image (as uploaded without crop). For ex: I have all the images of size 640 x 480 in the parse data base. So what should be the size of UIImageView frame in my UITableView cell?
I need aspect fit functionality. Aspect fit gives me the full image but I have to struggle to see which frame size to select. Is there a formula to get the frame size depending on the image size (for ex: 640 x 480). Frame to Pixel? I can get the desired functionality with trial setting different frame view size and running the app, but I want to know if there is a standard formula to get this size depending on the image size.
For the sake of simplicity, lets keep the image size 640 x 480. So what should be the UIImageView frame size in attribute inspector to get that image in full size without using Aspect fit.

Related

Resize an image using an image cropper in Umbraco 7.6

Update: I'll make some crops for the Umbraco's image cropper (Umbraco.ImageCropper) in Umbraco 7.6. The users uploads images over the 4MB (some images are over the 7MB). Now I'll crop resize this images to improve page speed.
I've seen that some of the images are taken in landscape and others in portrait. For a image gallery I'll have the original height and width ratio but with a max width of 500 pixels with a variable height.
I've tried to place the height empty and place to zero but both gives me an error:
I can only make crops with a fixed width and height. Now my questions are:
Could I give the crop a variable height dependent on the original size of the image?
Could I just resize the image to a fixed width?

Swift: is possible to get the default size of UIImageView used by UIImagePickerController?

In my iOS app I am developing, it is necessary for the user to take a photo. As you can see from the following image
by default, when we take a photo with iOS camera app, then it is showed with a standard/default size (the one delimited by those 8 pins) and I would like my app to be the same. So, which size should I set my UIImageView into which the photo will be displayed? How can I get that default size in Swift?
Or maybe...which would be the best size to give to UIImageview to prevent the photo from being deformed too much?
Thank you very much for you attention
UIImage has a property called size which specifies width and height of the image. So you could size your UIImageView to those.
Alternatively, if you're using constraints or autoresize mask (flexible width, flexible height) simply don't set a size and the UIImageView will fill itself according to contentMode.
You must understand however that what you see in the image you posted is not the "original size" of the image. Someone decided that the UIImageView should be place at X distance from top and bottom margins, thus forcing an implicit size on the UIImageView

UITableView cell auto height adjustment with image scaling issue iOS 9

I have a UITableView, which has a custom cell that I want to display a single image. I want the cell to automatically adjust the height according to the image's size. The UIImageView is filled in the cell, with auto layout constraints set on the four edges.
The images may be on different sizes, some are wider than the screen width. In such case, due to the content mode I set on the UIImageView, the image will be scaled to aspect fit the entire UIImageView.
When I set everything up and run, I realized that the UITableView didn't set the cell height correctly. It seemed that the image was scaled to fit the UIImageView but UITableView still set the cell height to be equal to the original image's height. Besides this issue, I also don't know why the image was scaled given that there are only 640 pixels on the width of the image and the iphone 6 simulator should have 750 pixels on the width ?
As summary, I have two questions here:
If I give an image with 640 pixels wide to an UIImageView that fills the entire width of the screen, why the image is scaled?
If the image is indeed scaled, how to let the UITableView automatically calculate the cell height based on the scaled size rather than the original size.
If possible, please use Swift instead of Objective-C for showing the code. Also you can assume I am using iOS 8+. Thank you.

Creating UIView with pre-defined size in pixels

Is it possible to create a custom UIView that will be rendered on for example 1500 x 1500 pixels? I can easily create an image from a UIView and save it, my problem starts when I'm using an iPhone 5 and the image I save will be 640 x 640 px (the UIView is a square) because 640 pixel is the maximum width of it's screen resolution. So when I use the same image on a iPhone 6 Plus it will be pixelated because its screen width is 1080 x 1080 px, therefore I need to create the images that looks good on every screens. How could I manage this? Or how could I make a UIView that will be the same on every device?
In this case the result will be different based on the type of the retina screen:
CGRect newFrame = self.fullView.frame;
newFrame.size.width = 1500;
newFrame.size.height = 1500;
[self.fullView setFrame:newFrame];
I have an idea, that I would use if statements to figure out the current device and use different sizes for different devices, but a cleaner solution would be amazing. However I'm not sure that it would be the proper
logic so I welcome any other solution.
I'm saving the UIView as a UIImage with UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0); so the saving method won't decrease the size.
Not sure exactly what you're trying to do but you should Auto Layout to make the view fit the correct screen size.
Also, if you want to have a specific image for each screen size you should use Asset Catalog where you can create an image set with the will load a different image for each device.

Again edit of edited UIImage in iOS app

In my iOS app, I am putting several UIImages on one back UIImage and then saving the overall back image with all subimages added on it by taking screenshot programmatically.
Now I want to change the subviews UIImages position of that saved image. So want to know how to detect the subview images position as I have taken whole image as screenshot.
Record their frame as converted to window coordinates. The pixels of the image should be the same as the frame origin (for normal) or double for retina. The screenshot is of the whole screen, so its dimensions are equivalent to the window frame. UIView has some convenience methods to convert arbitrary view frames to other view (or window) coordinates.
EDIT: to deal with content fit, you have to do the math yourself. You know the frame of the imageView, and you can ask the image for its size. Knowing the aspect ratio of each will let you determine in which dimension the image completely fits, and then you can compute the other dimension (which will be a value less than the imageView frame. Divide the difference of the view dimension minus the image dimension by two, and that lets you know the offset to the image inside the view. Now you can save the frame of the image as its displayed in the view.

Resources