I have an UIImageView which is as big as the whole view. When I insert an image, I would like for the image view to shrink itself in order for it to be as big as the image I insert itself. I cannot find a way to do it.
There are a number of ways you can approach this assuming you have a UIImageView declared like so:
#IBOutlet var dynamicImageView: UIImageView!
When changing your image, set the imageView size to that of the UIImage e.g:
func changeImage(){
if let image = UIImage(named: "Octocat"){
dynamicImageView.image = image
dynamicImageView.frame.size = image.size
print("Size Of ImageView = \(dynamicImageView.frame.size)")
}
}
Just use the .sizeToFit method e.g:
func changeImageAgain(){
if let image = UIImage(named: "Octocat"){
dynamicImageView.image = image
dynamicImageView.sizeToFit()
print("Size Of ImageView = \(dynamicImageView.frame.size)")
}
}
Running on an iPhone 7 Plus the UIImageView size is initially (375,647), and when applying either method the size changes to (800,665) which is the same size as the GitHub OctoCat:
I have created one demo app which will contains XIB with image view. Here I have written code to resize imageView as per image size. This code is working in my project, I shared link for demo app, you can download and test it.
In this project, I have implemented code to create imageView based on image size and vertically and horizontally centred aligned.
Also implemented code to zoom image.
https://app.box.com/s/sro2g7gdv5c67f9oj97gfhicjnpo2vqc
I hope this will work for you and as per your requirement.
Related
I have a tableViewCell with image view.
Even though I clear the imageView and set the image I get the below unintended behaviour, where one image is overlaid over the other.
Please advice how this could be resolved.
Code on Resetting Image in ImageView:
self.profileImageLabelTVCell.cellImageView.image = nil // Clearing previous image
self.profileImageLabelTVCell.cellImageView.image = image
Here is the link for the tableViewCell code and roundedImageView class, that I've used. (Since SO did not allow the entire code to be posted here)
https://gist.github.com/pravishanth/14cdb8bf14dd8899b081bdc97988b985
Add your reset image code to the TableViewCell's
func prepareForReuse() method.
override func prepareForReuse() {
super.prepareForReuse()
cellImageView.image = nil
}
I've got this code
override public func viewDidLoad() {
let imageBlankCard = UIImage(named: "BlankCard.png")
let imageViewBlankCard = UIImageView(image: imageBlankCard)
rockCard.addSubview(imageViewBlankCard)
scissorCard.addSubview(imageViewBlankCard)
paperCard.addSubview(imageViewBlankCard)
self.view.addSubview(rockCard)
self.view.addSubview(scissorCard)
self.view.addSubview(paperCard)
}
But when I run the playground, only the latest UIView (paperCard) is displayed properly, the other cards are not shown at all as you can see in the image here: there should be two cards like the last one, in place of the arrows.
Can someone help me? Thanks everybody!
According to the documentation,
Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.
This explains the behaviour that you're getting.
One solution to this is to create three separate image views and add each of them as a subview.
Judging by the variable names, I think you are creating something similar to a rock paper scissors game. And you have three views that each holds an image view with the image of a blank card. And you want to add the image of a stone, paper and scissors as an overlay to the image view. If that's what you want to do, why not just make rockCard, paperCard and scissorsCard themselves UIImageViews and set the image to be BalnkCard.png?
let imageBlankCard = UIImage(named: "BlankCard.png")
rockCard.image = imageBlankCard
scissorsCard.image = imageBlankCard
paperCard.image = imageBlankCard
So I am creating a forum. When a post includes an image I would like to keep the aspect ratio of the downloaded image while making the image's width constant at 75% of the cell's width.
I created an outlet for the imageHeightConstraint in my custom cell class and tried the code below in the cellForRowAtIndexPath method.
let newHeight = (image!.size.height/image!.size.width)*cell.uploadedImageView.frame.width
cell.imageHeightConstraint.constant = newHeight//Calculate the new height
The code above doesn't adjust the image size at all.
Currently I have the constraint for image width to be 75% of cell's width and height equal 200.
I also already have all the code to adjust the tableviewcell automatically depending on content and that is working fine.
Thanks for any help.
Try
if let image = image {
cell.image.heightAnchor.constraint(equalTo:cell.image.widthAnchor, multiplier: image.size.height/image.size.width).isActive = true
}
If you want to also limit the height to 200, then you can also add
cell.image.heightAnchor.constraint(lessThanOrEqualToConstant:200).isActive=true
I'm trying to add a title image to my nav bar in the entirety of my app. The below line of code in my App Delegate works, but the image is tiled to fit... Is there a way I can set the dimensions and position?
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "tmp logo.png"), forBarMetrics: .Default)
You can set the size of image, by setting the size of UIImageView.
var imageNew = UIImageView(frame: CGRectMake(10, 10, 100, 100));// define
the size of the view here.
var image = UIImage(named: "myImage.png");
imageNew.image = image;
self.view.addSubview(imageNew);
Pl. refer to the below stackoverflow response, which provides the solution
How do set a width and height of an Image in Swift
How can I display image inside UIImageview? Then change images according to pressed button.
I am very new please go easy.
To do this, first make sure you have the image available in the viewcontroller (by creating an association between the image on your storyboard, and in your viewcontroller).
Let's pretend it's called imageView.
Programmatically, you can say:
imageView.image = UIImage(named: ("nameofasset"))
What I actually ended up doing is creating a static function responsible for determining which image to return as such:
class func GetImage(someValueWhichDrivesLogic: Int)->UIImage
{
if (someValueWhichDrivesLogic == 1)
{
assetName = "imageone" //note you don't add .png, you just give it the same name you have in your standard images folder in XCode6
}
else if (someValueWhichDrivesLogic == 2)
{
assetName = "imagetwo" //again, this is the actual name of my image
}
//Return an image constructed from what existed in my images folder based on logic above
return UIImage(named: (assetName))
}
So now that the function above has been created, you could just do something like this:
imageView.image = GetImage(1)
imageView.image = GetImage(2)
This would actually assign the images dynamically to the image. You could just do this, or if you're literally doing something as simple as putting it into a button click you can just take the easier approach I specified above.
Thanks let me know if any questions!