Where to add image layer for xcode objects? - ios

I want to add a border around my UIimageView?
Let's say everytime I add a new photo:
flagImageView.image = UIImage(named: correctAnswer)
the photo is different, and I need to re-scale.
Where do I put the code, although its not the right answer. I am unsure how to rescale the imageview
// Define a new image object
UIImage *image = [UIImage imageNamed:#"lake.png"];
// Define a new image view
UIImageView* imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 157, 125)];
// Set image in imageview and add to view
[imgView setImage:image];
[[self view] addSubview:imgView];
// Create a white border with defined width
imgView.layer.borderColor = [UIColor whiteColor].CGColor;
imgView.layer.borderWidth = 1.5;
// Set image corner radius
imgView.layer.cornerRadius = 5.0;
// To enable corners to be "clipped"
[imgView setClipsToBounds:YES];
I have a code that adds a new image, with different size to a UIimageView. How would I add an image? Is that in a subclass, or viewController, or model?

this would be the code in swift:
override func viewDidLoad() {
super.viewDidLoad()
// Define a new image object
let image = UIImage(named: "massage.jpg")
// Define a new image view
let imgView = UIImageView(frame: CGRect(x: 40, y: 40, width: 157, height: 125))
// Set image in imageview and add to view
imgView.image = image
self.view.addSubview(imgView)
// Create a white border with defined width
imgView.layer.borderColor = UIColor.redColor().CGColor
imgView.layer.borderWidth = 1.5
imgView.layer.cornerRadius = 5.0;
imgView.layer.masksToBounds = true
// Do any additional setup after loading the view.
}
this is how it would look like:
viewDidLoad is called when the controller loads it's view, which is not necessarily right after initialisation.
viewWillAppear is called just before the view is displayed.viewWillAppear is called every time the view is displayed;

Related

how to set frame for cell.imageView.frame?

i tried this below code in cell for row...
if([cell.textLabel.text isEqualToString:#"Home"])
{
cell.imageView.image = [UIImage imageNamed:#"Home-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
This is below code is in didselect row at Indexpath...
if ([[arrCell objectAtIndex:indexPath.row] isEqualToString:#"Home"]) {
cell.imageView.image = [UIImage imageNamed:#"Home1-50.png"];
cell.imageView.frame=CGRectMake( 10, 15, 20, 20 );
}
I am using this above code to display image in tableview cell and when cell is clicked Then Image change to another image...Images are Changing But,the frames are not applying...
No you can't do this because it is fixed layout of UITableViewCell.If you want to do this go with below option
OPTION 1: CustomCell
Through CustomCell you can set image view frame whatever you want.
OPTION 2: Programmatically creating image view add it to cell
UIImageView *img = [[UIImageView alloc] init];
img.image = [UIImage imageNamed:#"yourImageName.png"];
[img setFrame:CGRectMake( 10, 15, 20, 20)];
[[cell.contentView addSubview:img];];
Can be achieved by transform scalingļ¼š
cell.imageView?.image = UIImage(named: "icon_shareToFriends")
if let imageView = cell.imageView, imageView.transform == .identity {
imageView.transform = imageView.transform.scaledBy(x: 0.55, y: 0.55)
imageView.contentMode = .center
}

UIViewContentModeScaleAspectFit doesn't work

I try to scale my image and i want use Aspect Fit. But it doesn't work
solutionImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bg_solution#2x.png"]];
// optional:
solutionImageView.contentMode = UIViewContentModeScaleAspectFit; // content mode
solutionImageView.center = CGPointMake((currentWidth)/2, currentHeight - (currentHeight/3.5));
[self.view addSubview:solutionImageView];
As arunit21 said in his comment, when you create an UIImageView with the initWithImage method, the size of frame of the view is calculated with image size. So your image view is centered but bigger than you window.
You can do
solutionImageView = [[UIImageView alloc] initWithFrame:yourFrame];
solutionImageView.image = [UIImage imageNamed:#"bg_solution"];
solutionImageView.contentMode = UIViewContentModeScaleAspectFit; // content mode
solutionImageView.center = CGPointMake((currentWidth)/2, currentHeight - (currentHeight/3.5));
[self.view addSubview:solutionImageView];
You can also just set your new frame after your initialization to set the correct size.
The #2x.pngis not needed in [UIImage imageNamed:#"bg_solution"]. With this method, the system will check if the screen is retina and look for the #2ximage if it exists and take the normal one otherwise.

Issue with subviews overlapping

I'm trying to create a custom view which containes several images. I do that by adding them programmatically. The problem is that those subviews overlap each other and I can't find the way to change that. The only solution I can see is doing something like setting frames for each new image programmatically. I would be grateful if someone could tell me what is the best way to solve this issue.
for (id image in self.images) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.imageViews addObject:imageView];
[self addSubview:imageView];
}
If you wanna make your customView like UICollectionView you need a UIScrollView and add your subviews in it. Everytime when you add a subview change frame location so it could be something like this:
int xPosition = 0;
int yPosition =0;
for (id image in self.images) {
if (xPosition>self.view.frame.size.width) {
//give size to every imageView and every time in loop change the location
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, yPosition, 50, 50)];
imageView.image = image;
yPosition = yPosition + 50;
[self.view addSubView:imageView];
}
else {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPosition, yPosition, 50, 50)];
imageView.image = image;
xPosition = xPosition + 50;
[self.view addSubView:imageView];
}
}
Without using Interface Builder your only real options are to change the frame or the center.
imageView.frame = CGRectMake(x coord, y coord, width, height);
This method lets you resize and move whereas changing the center lets you do just that, move the center of the view.
or
imageView.center = CGPointMake(x coord, y coord);
Or as recommended add constraints.

Add label at certain point on top of an UImage

I have a ViewController that has a UIImageView as a background. What is the proper way to add a label on top of the image if I have the coordinates? My program is adding additional images on top of the UIImageView with:
UIGraphicsBeginImageContext(self.myBackground.image.size);
[newImage drawAtPoint: point];
// Draw a textlabel below the new image
// Some additional code
I have managed to draw the new Image on top of the original, but I don't know how to add the label.
Hank
[#"YOUR TEXT" drawAtPoint:point withFont:[UIFont boldSystemFontOfSize:15.0]];
You can use addSubview: method for UIImageView to add label or any other subview.
Set up label frame to be relative to uiimageview and add it as a subview:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
// To use point you can specify label size and after that you can set center:
label.center = point;
//set up other label properties
[self.myBackground addSubview:label];
//I assume self.myBackground is type of UIImageView

Stopping UIScrollView at specific place while scrolling with pagingEnabled

I have the following code to create a scroll view with additional area at the beginning and the end of the images (50 points).
UIScrollView* scroll = [[UIScrollView alloc] initWithFrame: CGRectMake(0,0,200,100)];
scroll.contentSize = CGSizeMake(400,100);
UIImageView* img1 = [[UIImageView alloc] initWithFrame: CGRectMake(50,0,100,100);
UIImageView* img2 = [[UIImageView alloc] initWithFrame: CGRectMake(150,0,100,100);
UIImageView* img3 = [[UIImageView alloc] initWithFrame: CGRectMake(250,0,100,100);
//Adding images to ImageViews
scroll.pagingEnabled = YES;
[scroll addSubView:img1];
[scroll addSubView:img2];
[scroll addSubView:img3];
The first time I see the view, I will see the additional area on the left (0-50), then the first image (50-150) and then half of the second image (150-200).
When I swipe left, I want to see half of the first image on the right, the second image at the center, and half of the third image on the right.
When I swipe left again, I want to see the third image at center, with half of the second image on the left, and the additional area on the right.
Can it be possible?
It's possible as long as you define everything correctly. Make sure that the assigned content size is equal to the overall size that you wish to scroll through. Make sure that each page size is equal to the scroll view's frame. You can set the clipsToBounds to NO if the frame clips your subviews.
You can do this by adjusting the contentSize of UIScrollView
- (void)addImagesToScrollView{
//An offset from where imageViewStarts
CGFloat xOffset = 50.0f;
CGRect imageViewFrame = CGRectMake(xOffset, 0.0f, 100.0f, 100.0f);
for (NSString *imageName in #[#"image1.jpg",#"image2.jpg",#"image3.jpg"])
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:imageViewFrame];
UIImage *image = [UIImage imageNamed:imageName];
imageView.image = image;
[self.scrollView addSubview:imageView];
imageViewFrame.origin.x+=imageViewFrame.size.width;
}
CGSize contentSize = self.scrollView.frame.size;
//Content size is calculate from the imageViewFrame and an offset is added to it
contentSize.width = imageViewFrame.origin.x+xOffset;
[self.scrollView setContentSize:contentSize];
}
Source Code
You can scroll to a particular place in the scrollview using the following code
[YOURSCROLLVIEW setContentOffset:CGPointMake(x, y) animated:YES];

Resources