Blurry images after using setContentMode with UIViewContentModeScaleToFill - ios

UIImageView *assetImageView = [[UIImageView alloc] initWithFrame:viewFrames];
[assetImageView setContentMode:UIViewContentModeScaleToFill];
[assetImageView setImage:[UIImage imageWithCGImage:[self.asset thumbnail]]];
[self addSubview:assetImageView];
This code generate a blurry image because of the reduction in quality and
I found that I need to use this
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
But how do i apply this code in my case?

I'd recommend reading this article. Use the code there to resize the image to the right size before passing it to UIImageView.

Related

Xcode: Image becomes filled with color on UIBarItem while scaling, function initeWithImage(UIImage)

please, help me to find some solution to the following problem. IOS, Xcode.
I have UIBarItem component - the button that switches languages on the toolbar. The bar has function initeWithImage(UIImage). The problem is when i am trying to scale the image it becomes filled with color...The picture is too big for the toolbar. I have changed the size of the picture manually, but it looks ugly (i need scaling).
I have tried to solve the problem:
1. trough Scale
2. through Frame size
3. and variations of the two above :)
Some code:
self initWithImage:image //
style: UIBarButtonItemStyleBordered //UIBarButtonItemStylePlain
target:target
action:action
UIImage *image;
image = [[UIImage imageNamed:#“image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
-(UIImage )imageWithImages:(UIImage )image scaledToSize:(CGSize)newSize {
//UIGraphicsBeginImageContext(newSize);
// In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
// Pass 1.0 to force exact pixel size.
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Please, help me with this. Quite simple but tricky task. I have already spent several hours with that... Thank you in advance :)
You should not use initWithImage method.
If you want to display the exact image you should use initWithCustomView method.
For example:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image"]];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:imageView];
You can scale down the image before setting it to the image view if you want to.

Parse Background image in loginView, not fitting to fit all iPhone device screen sizes

I am trying to get my background image to fit all screen sizes within Parse's logInView. Unfortunately this code isn't doing the trick I expect it to.
[self.logInView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Backgroundwithlogo.png"]]];
I have subclassed the PFLoginViewController to what I just called LoginViewController. I have tried resizing the image that i imported, but it either adjusts itself to be too big, or too small. The colorWithPatternImage will tile my image across the view if it's too small, and my background image seems to be stretched horizontally right now.
What I have tried:
I have tried implementing:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Backgroundwithlogo.png"]];
[self.logInView addSubview:imgView];
[self.logInView sendSubviewToBack:imgView];
imgView.contentMode = UIViewContentModeScaleAspectFit;`
with no avail. I have read the Parse Guidelines Here. But they don't cover this issue with sizing problems inside the UI. Any help would be greatly appreciated.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imgView.image = [UIImage imageNamed:#"Backgroundwithlogo.png"];
[self.logInView addSubview:imgView];
[self.logInView sendSubviewToBack:imgView];
Also, you may need a specific contentMode.

Content Stretch Deprecated

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MDSpreadViewCell.png"]];
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.contentStretch = CGRectMake(2./imageView.frame.size.width, 2./imageView.frame.size.height, 1./imageView.frame.size.width, 1./imageView.frame.size.height);
What is the equivalent code for the contentStretch of the image?
Use a resizable image (resizableImageWithCapInsets:resizingMode:). In Xcode 5 you can use a sliced image (set up in the asset catalog) for even more power.
There is a layer equivalent of contentStretch (contentsCenter) but I don't think that's a good idea with an image view.

How to add large background image on the UIView in a full size?

I have a high resolution image and I want to use it as a background for a view. But when I add it either via an Interface Builder or programatically I see only its part.This doesn't help:
UIImage* _backGround = [UIImage imageNamed:#"background-clean#2x.png"];
CGRect _viewFrame = self.view.frame;
[_backGround drawInRect:_viewFrame];
UIImageView* _backGroundView = [[UIImageView alloc] initWithImage:_backGround];
[self.view addSubview:_backGroundView];
[self.view sendSubviewToBack:_backGroundView];
And this too:
_backGroundView.contentMode =UIViewContentModeScaleAspectFit;
So the question how can I scale this image in order it fits in the view in a full size in spite of its size?
P.S. Sorry for my bad English.
I agree with Ali, when creating retina-sized images you should also scale them and add their smaller version to your project. With both versions you can simply specify the full name of the image without the #2x.png or .png extension.
But, to fix your implementation you only need one extra line of code:
UIImage* _backGround = [UIImage imageNamed:#"background-clean"];
UIImageView* _backGroundView = [[UIImageView alloc] initWithImage:_backGround];
_backGroundView.frame = self.view.frame;
[self.view addSubview:_backGroundView];
[self.view sendSubviewToBack:_backGroundView];
Once you change the frame of the UIImageView, its contents (i.e. your image) will scale as well.
You must not include #2x in your code when referencing the name of the image!
You should:
Include both files Background.png and Background#2x.png in your Xcode projet resources, Background#2x.png being twice the size (in pixels) of Background.png
But always refer to this file (for example in imageNamed:) as #"Background.png" or even just #"Background": [UIImage imageNamed:#"Background"]
Then iOS will do its magic all by itself, choosing the right image between Background.png or Background#2x.png depending on if the device screen is Retina or not.
For more info see here in Apple documentation.
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background-clean"]]];

Creating a UIImageVIew with the exact size of an image

How is the size detected from an image and then use that size to create a UIImageView, perfectly fitting the image inside?
Just create the UIImageView like this:
UIImageView *aImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourImage.png"]];
So the ImageView has the size of the image. From the Documentation of UIImageView:
This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default.
I hope my answer helps you. :-D
Sandro
Here no need to set the frame size when we use the image in imageView
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
ImageView will automatically takes the size of image.

Resources