Creating UIView with pre-defined size in pixels - ios

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.

Related

Image size for universal app ios?

I have a question about using images for universal apps on IOS.
I've created a universal app that works on all iphone devices and ipads. I've placed all the content via storyboard. On a View I have two buttons one on the top space area that is 40 points high and as wide as the view, one on the bottom area also 40 points high and as wide as the view and an image that is a square (A x A) in the middle of the view, the image is constrained by horizontal and vertical (For it to stay always aligned in the middle of the view).
(I understand that if I use an image that is 100 points by 100 points image at 1x, I need to create two other images at 200 points by 200 points for 2x and 300 by 300 for 3x.)
1) So if I constrain the UIImage container by height and width equal to 100 by 100, it will be 100 by 100 points in all devices. But I want it to use as much space as possible. That would mean that on iphone 5 - 6s+ it would always be 100 by 100, it would look smaller on every growing screen. It would also mean that I would need to make it bigger for Ipad (Storyboard changing to regular regular to change UIImage container size for ipad, ex. increase it to 300 x 300). Put once I make it bigger, the image I have will be to small for that size therefore turn blurry or pixelated.
Right?
2) If I would like to be able to use as much space as possible I thought of using the following method. Constraining the UIImage to be equal width as the view but reducing its multiplier to 0.9 or 0.8 (Therefore making it smaller then the view width) and placing an aspect ratio of 1:1(To maintain it square). That way taking advantage of most of the view space that is free, and In all the devices it would always seem filling the same space. However the problem would be that the image would have to be different sizes. (Ex. iphone 5 = 150 x 150, on iphone 6 = 250 x 250, on iphone 6+ = 320 x 320 ans on ipad 600 x 600).
so If I make an image that in 2x that is 150 x 150, when its used on the iphone 6 it would be distorted or pixelated and the same for for the rest.
So can someone help me understand what I should do? or link a tutorial?
Please help!!
I've found that the best way to solve this problem would be to make a much larger image and let the constraints resize it. That way, you've covered your bases if new resolutions are developed, or with the current varying sizes. A much larger image would not look bad if it was compressed into a smaller space, but you might lose some details.

How to resize UIImage vector graphic

This is a rather strange question. You're probably thinking "You must resize the UIImageView, not the UIImage", but I can't get it working, and I'm not sure it's correct in this case.
I have a .pdf vector image I want to use in an iOS application. This vector image was exported to .pdf with the default size of 320 width. Of course, this is vector, so it should scale perfectly.
When I create an UIImageView using that image (UIImageView(image: vectorImage)) it always gets a width of 320 (or scaled #2x/#3x).
I have constrained the UIImageView to have margin 0 left and right. On iPhone 5, this displays correctly, since it's a 320-based size screen. However, on iPhone 6 - the resolution is different, with a 375-based screen width.
Even though the imageView is constrained to the full width (375), the UIImage within still has a size of 320.
I never set the height.
To clarify, I'm using ScaleAspectFit. This should not cause the image to be stuck at 320 though. As I never set the height of the UIImageView, it should begin with a correct fit.
I can probably solve this if I have the aspect ratio for the image, but that seems unnecessary!
A correct result would be for the image to fill the entire width of the imageView, and the height of the imageView to change to fit the image.
The actual result is that the height and width of the UIImage is the same for all devices (relatively), and the height for the UIImageView is the same for all devices. Due to the constraints added, the width of the UIImageView is different, but the UIImage within is not changed. Why not?
I suspect that if the vector-image had been exported with a different default size, the result would be different. Why is this?
This post gets a lot of views so maybe worth a quick answer in case it helps anyone.
When using vectors you can't set the image and to the UIImageView and expect it to work out its height from the width, even when setting it to ScaleAspectFit.
One way to resolve this is to get the UIImage, get it's size and then use this to set the UIImageView height constraint.
For example:
let image = UIImage(named: "name")!
let ratio = image.size.height / image.size.width
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor, multiplier: ratio).isActive = true

iOS Swift: Match UIImage frame size with Image pixel size

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.

UIImage rendering not clear on iPad Mini

The following code block is used in my application to take a screenshot of the current screen of an iPad mini(768 x 1024):
UIImage *img;
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
In a different viewcontroller, I present a UIScrollView with a width of 540 and a height of 290. I display the screencapture UIImage in a UIImageView which I create programmatically initWithFrame with a rectangle width of 250 and height of 250. The content size of the scrollview is 768 by 250.
Now running the application, I display four rectangles and screenshot the screen using the above block of code. Transitioning to the UIScrollView, the image is not clear (and by not clear, some rectangles are missing sides while some are thicker than others). Is there a way to display the image clearer? I know the image has to be scaled down from the original 768 by 1024 to 250 by 250. Could this be the problem? If so, what would be the best fix?
Edit:
Above a screenshot of the image I want to capture.
Below is the UIImage in UIImageView within a UIScrollView:
Cast each coordinate to int, or use CGRectIntegral, to do that directly on a CGRect, decimal point requires AA and makes images blurry.
Try changing the content mode of your UIImageViews. If you use UIViewContentModeScaleAspectFill, you shouldn't see any extra space around the edges.
Update: From the screenshots you posted, it looks like this is just an effect of the built-in downscaling in UIKit. Try manually downscaling the image to fit using Core Graphics first. Alternatively, you might want to use something like the CILanczosScaleTransform Core Image filter (iOS 6+).

Retina & non-retina compatible UIImage stacking and positioning

I often need to position and stack two UIImages into an UIView.
For example, placing a picture frame around (behind) a photo, and then displaying it. Specifying the position by modifying the image's frame will not work with both Retina and non-retina devices because the hardcoded frame values will not scale.
For example, if I have a photo frame who's image is 56x56 (retina) and the actual image is 52x52 (retina), it needs to be placed at CGRectMake(2, 2, 54, 54) on top of the photo frame. But if the user is on non-retina this will fail since those values will be incorrect (they're now CGRectMake(1, 1, 27, 27)).
What is the correct way for resolution-safe image processing? Is there a way to combine two images (each with regular and #2x.pngs) that will display correctly on either screen type?
You can use UIScreen scale property. It's a readonly property. In devices prior to iPhone 4 (non-retina displays) this value will be 1.0, but with retina displays the value is 2.0. So you can store this property to a local variable, say called CGFloat scaleFactor and multiply it with the values above:
CGFloat scaleFactor=[[UIScreen mainscreen] scale];
... = CGRectMake(1.0*scalefactor,1.0*scaleFactor,27.0*scaleFactor,27.0*scaleFactor);

Resources