I am using UIImage inside a ScrollView to zoom an image. But zooming reduces the quality of the image. My original image dimensions are 1200x1600 pixel and UIImage rect are 1024x768 pixel. How can I maintain the quality of the image in such cases.
Any zoom scale beyond 1.0x reduces the image quality. If you want absolutely no degrade in quality you should set the maximumZoomScale property of your scroll view to 1.0.
In your case, that means you can zoom your image from 576x768 (aspect fit of your scroll view) to 1200x1600 (its original size), approximately 2x.
Related
If I have a large UIImage that need to be displayed on a smaller UIImageView.Should I resize the image first or should I count on the contentMode of UIImageView?
You can set the content mode as Scale To Fill maintaining the aspect ratio of UIImageView so that it may not feel like getting stretched.
I am trying to export an image from a UIView that contains a UIImage view and some labels. I am not sure I am going about this the right way. I want to export everything in the view and maintain the layout. I want to export at 1536 x 2048.
I am using the following code with renderInContext to grab an image of the main view (UIView). Kind of works, but the layout gets messed up, basically the layout changes and the labels do not scale properly. Is renderInContext the right way to go for something like this? Is there a better way?
you can download the whole project here: http://tinyurl.com/7qvhqtp
UIGraphicsBeginImageContext(CGSizeMake(1536, 2048));
viewOutput.frame = CGRectMake(0, 0, 1536, 2048);
[[viewOutput layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(image,nil,nil,nil);
This is the code I use to save the current UIView as an image. The layout gets saved perfectly.
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I'm not 100% sure, but there might be some issues with UIGraphicsBeginImageContext vs UIGraphicsBeginImageContextWithOptions.
On the other hand the problem might be in your view's subview's autoresizing mask if your view has the autoresizesSubviews set to YES. I'd try to disable it if you do not have the support for rotations or at least before changing your view's frame.
The only thing you did to get a bigger image was to change the frame but the labels did not have the correct autoresize masks nor did their font size change. You can clearly see this in the "after running code" image that the label did neither move nor change size.
First, If you wan the label to change its frame when the superview's frame changes (are the labels subviews of the image? If not you may need to calculate their new frames yourself) then you should give it a suitable auroresizing mask. In your case you would want it to have a fixed distance to the bottom of the screen and a flexible distance to the top. You would also want the distances to the left and right to be fixed so that the width can be flexible.
This only leaves you with the font size problem. You should calculate the scale factor (how many times bigger everything gets) and multiply the current font size with that scale factor.
I have an image of width 200 and height 200.when I rotate it to some degree say 60 degree then new width and height comes out to be 273,273.
But I want to retrieve the same height and width 200*200 whatever rotation it has.
I am trying to scale that image to some ratio of back view size on which that image is placed. If back view reduces to half the image size is also reduced to half and same way if back view is increased to double image also increased to double. But when I rotate the image and try to change the frame of back view that image resizing works fine if it’s not rotated but if it’s rotated then resizing of back view ratio will not work fine with image ratio. Image either increase more or decrease. Generally it increases more than expected.
view.bounds.size would not change after the rotation
Image.transform=CGAffineTransformMakeRotation(angle); works perfectly fine . Check your image .... if it is in ping or jpeg etc. The issue is with the image I think not with the method .. :)
Question - How do I scale the image in a UIImageView when in "centre" mode? (change frame doesn't work) I should clarify in that I would like to be able to do this programmatically.
I can scale the UIImageView by changing it's frame to something smaller but on and same center, and this works (I put a border on view to check), however the actual image in the UIImageView doesn't change. Remember the UIImageView mode has got to be "Centered".
Background: the reason I ask is because I have some images I want to scale between orientation changes, and they need to be centered, however their views can NOT have a "aspect fit" mode as some of the views are being rotated over time using a transformation, and having them auto-scale as orientation changes occur as they rotate won't work well.
Try to set the contentScaleFactor (available in iOS 4.0) or the transform property:
imageView.contentScaleFactor = scale;
imageView.transform = CGAffineTransformMakeScale(scale,scale);
What will be best or standard aspect ratio of image to fill view using UIViewContentModeScaleAspectFit, or what'll be best resolution of image to fill view???
As for as I know, you've no need to worry about the aspect ratio of image. If you set the content mode of the view to UIViewContentModeScaleAspectFit, then it
Scales the content to fit the size of the view. This option maintains the aspect ratio of the content. Any remaining area of the view’s bounds is transparent.
If the image size is 60x40, and your views size is 200x200, the image will be displayed in the size 180x120, centered on its superview. And your image may loose some details if its superview is too large to fit.