UIImageView shows a discrete image when created programmatically - ios

I've got the problem. I need to create the UIImageView programmatically and add the image to it. But there are some issues with it. Look at the attached screenshots.
The first is the normal image, if I create the UIImageView in xib (storyboard) and fill it with my image.
The next one is the image with some not smooth lines (like a discrete or blocks or etc).
The code I've made
UIImageView *logoImg = [[UIImageView alloc]initWithFrame:CGRectMake(76.0f, 36.0f, 168.0f, 180.0f)];
logoImg.image = [[UIImage imageNamed:#"logo.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.view addSubview:logoImg];
I have got two versions of this logo image (logo.png and logo#2x.png for non-retina/retina).
logo.png is 168x180 px.
logo#2x.png is 336x360 px.
Where I was wrong? Thanks in advance :)

I'd recommend deleting both images from the project, cleaning the build folder, restarting xCode and re-adding the images.

Not sure but try with
myImageView.contentMode = UIViewContentModeScaleAspectFit;
This will display original image, may be work for you :)

Related

Add a text to an UIImage

I have an UIImage I want to allow the user to circle a particular part of the image and then write something about it on the image and then I want to save this image.
I do not know how to start on this are there any libraries or example that can help
First take one UIImageView add your image in it. Now add label as subview in UIImageView.
Now finally capture the the UIView as image.
Here this will help you to code for capture the UIView as image
Thanks.
Capture UIView and Save as Image

How to avoid distort and stretch image in Custom UIButton while using vector image set?

Below is my storyboard, I had quite a lot of constrains to make those four buttons fit into the screen nicely. However, after I put the images into the buttons and check the storyboard preview, all images are distorted. I try to adjust (had tried all of the options) the view mode at attribute inspector panel but seems no effect at all.
One more thing, I am using PDF as vector type image set, not regular .png file I am wonder if that matters? Should I change the way vector image displayed inside UIButton programaticly?
The verctor image size was 500px*500px. I resize the image into 50px*50px after #orkenstein 's reminader, xcode may not be able to handle oversized vector image that well. The image appear inside button as it is exactly 50px*50px and lost its ability to scale. Also as he sugested I should but the vector image inside an UIImageView and after my test, it works perfectly fine in an UIImageView.
Setting the contentMode on the button itself doesn't work for me, but setting it on the button's imageView does. From this Stackoverflow answer:
button.imageView?.contentMode = .scaleAspectFit

How to correctly set background for retina?

May be the question is elementary, but I have a bug with setBackgroundColor.
I have the UIView which contain UITableView. UITableView is clearColored. I'm trying to set background on UIView:
[self.viewForTable setBackgroundColor: [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgIngrList.png"]]];
On normal iPad it's looks like:
But on retina iPad (like the second image began showing):
I have two images "bgIngrList.png" and "bgIngrList#2x.png" with 290x260 and 580x520.
Where can be the bug?
EDIT
I have solved the problem by using UIImageView instead of UIView.
Thanks to everybody!!!
colorWithPatternImage: (as the name suggests) thinks your image as pattern to fill. so if the image you give is smaller than the size of the view, its gonna draw the pattern image again rather than stretching it. that is what happening here. your image's size is smaller than view's size hence its drawing the image again at the bottom. You can avoid this by adding a UIImageView and setting the image property rather than setting the backgroundColor of UIView.

IOS - Adding Image to UIImageView Causes Issues with Image Mode

Recently I have observed some strange behaviour with the UIImageView and am wondering if anyone can provide some insite. I have two UIImageViews with the images inside them being loaded from a database. Inside the Xcode inspector I set the clip subview property and change the image mode to aspect fill. This works fine, when the images are loaded they appear correctly in their views and are clipped as they should be. However, I wanted to add a place holder image incase the user hadn't uploaded one yet. When I did this it the image was not resized and and stretched passed its view. I tried to reset the properties programatically as can be seen below, but nothing changed.
//code used to try to reset the image mode and clipping of the sub-views
//set up user profile picture
_profilePic.contentMode = UIViewContentModeScaleAspectFit;
_profilePic.layer.masksToBounds = YES;
_profilePic.layer.borderColor = [UIColor whiteColor].CGColor;
_profilePic.layer.borderWidth = 3.0f;
//set up users cover photo
_coverPhoto.contentMode = UIViewContentModeScaleAspectFit;
_coverPhoto.clipsToBounds = YES;
_coverPhoto.layer.masksToBounds = YES;
Why is this happening when I add an image to the view through the Xcode inspector but not when the image is left empty?
You should use UIViewContentModeScaleAspectFill instead of UIViewContentModeScaleAspectFit.
When I add them with the following line of code at the beginning of ViewDidLoad I get the same effect and have none of the unwanted image overflow.
_coverPhoto.image = [UIImage imageNamed:#"stockCoverPhoto.png"];
_profilePic.image = [UIImage imageNamed:#"stockProfilePhoto.png"];
Not the most informative solution, as it provides no insight into this issue but it is a solution. Anyone have any other input on how to fix this issue, why it even happens I would love to hear it as im still scratching my head.
Same on here.. No way to change contentMode after setImage to UIImageView..

iOS app folders

I'm looking for an idea in order to reproduce the look of the iOS app folders (menu on iPhone/iPad).
I'm wondering how is it possible to separate the background image in order to make appear the content of the folder.
Does someone already do this ? Or have an idea on how we can do ?
EDIT : I'm trying to reproduce this effect.
http://img4.hostingpics.net/pics/873912photo.png
I actually looked at this a while back.
This is how i approached the problem:
You have a background view (the image) and a layer on top (the icons).
Calculate the split line.
Split the view and layer into tow along the split line
Add the content of the folder under the image.
Add you folder icon on top of everything.
Animate the bottom part down while changing opacity of the layer icon.( so you can see the content underneath)
While animating you should probably mask the triangle under the icon to it gives that bubble effect.
To close, just reverse the animation.
I didn't manage to make it work 100% but i can tell you it works (at least some parts of it).
And before you ask, no, i cannot give you the code because i didn't keep it.
Try make screenshot like this:
UIGraphicsBeginImageContext(CGSizeMake(screenWidth, screenHeight));
[self.view.layer renderInContext: UIGraphicsGetCurrentContext()];
UIImageView* imageView = [[UIImageView alloc] initWithImage: UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
[self.view addSubview: imageView];
You can make two screenshots and add with different frames as subviews.
I hope it helps you.

Resources