I need to display the image pass in from the another ViewController
first,I have tested using only ImageView to display the image and it works as below:
Thanks. Your help is greatly appreciated.
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var TempImgView: UIImgeView!
var passInImg: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
TempImgView.image = passInImg!
}
When I use UIScrollView ( for scrolling the large passInImg) , below code not working
Probelm:
error : fatal error: unexpectedly found nil while unwrapping an
optional value
But I have tested the pass in image in the above code.
what need to be done with the scrollView?
Should be used in viewDidLoad?
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var myUIScrollView: UIScrollView!
var myImgView: UIImageView!
var passInImg: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
self.myUIScrollView.maximumZoomScale = 10.0
self.myUIScrollView.minimumZoomScale = 1.0
self.myUIScrollView.delegate = self
myImgView.image = passInImg!
self.myUIScrollView.addSubview(myImgView)
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return myImgView
}
First check if myUIScrollView is connected to an outlet in storyboard and always check before using an optional variable:
if let img = passInImg {
myImgView.image = img
}
The difference is the way you are declaring the UIImageView.
In the first case:
#IBOutlet weak var TempImgView: UIImageView!
TempImgView is an #IBOutlet and hence initialised whenever the storyboard view/xib is loaded.
But then when you are using the scrollview, you have declared the UIImageView as a class variable and not #IBOutlet.
var myImgView: UIImageView!
It has to initialised before you try to access it.
Try:
myImgView = UIImageView()
myImgView.image = passInImg!
Please initialize the myImgView with frame before set the image.
It return nil because the myImgView not initalize
myImgView = UIImageView(frame:CGRectMake(10, 50, 100, 300));
It works for me. Hope it will helps you.Thanks alot
Related
I am working with swift UIVisualEffectView with mask option.
I am trying to make a blurred UIVisualEffectView masked by UIImageView.
I've set autolayout constraints to the views at the storyboard but the result varies through the iOS device.
Storyboard, code and results are attached below.
I would be glad if anyone help me.
I've set all auto layout constraints at the storyboard.(Capture included below)
Code is very simple. I've only set blurView.mask = topImageView.
class TestViewController: UIViewController {
#IBOutlet weak var bottomImageView: UIImageView!
#IBOutlet weak var topImageView: UIImageView!
#IBOutlet weak var blurView: UIVisualEffectView!
override func viewDidLoad() {
super.viewDidLoad()
blurView.mask = topImageView
}
// Still same issue when I move the mask to 'viewDidLayoutSubviews()' method
//
// override func viewDidLayoutSubviews() {
// super.viewDidLoad()
// blurView.mask = topImageView
// }
}
Storyboard and Code capture
Results depending on iOS versions
---
I've tried to move mask code from viewDidLoad() method to viewDidLayoutSubviews() but it's still same.
Self-solved by the below code.
Received a hint from similar issue : 'https://stackoverflow.com/questions/58998541/swift-mask-uiview-constraints-issues/59037761'
override func viewDidAppear() {
self.setupBlur()
}
I created an outlet by draggin & dropping an UIImageView into the ViewController.swift file
#IBOutlet weak var imageView: UIImageView!
I want to set the property of imageView to
imageView.contentMode = .scaleToFill
imageView.clipsToBounds = true
Doing this
#IBOutlet weak var imageView: UIImageView! {
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}
XCode complains that
#IBOutlet attribute requires property to be mutable
How do I go on about this?
What you're trying to do is a perfectly acceptable way to set up an #IBOutlet's properties… just don't reference other #IBOutlets, or the main view, etc, as this may force the main view to load earlier than expected.
You just need to wrap your code inside a didSet block…
#IBOutlet weak var imageView: UIImageView! {
didSet {
imageView.contentMode = .scaleToFill
imageView.clipsToBounds = true
}
}
An outlet is basically just a link to that UI element in the storyboard.
So you link your UIImageView in the storyboard to an element in your UIViewController code and then you can access and change properties of that element once it is loaded.
The Problem
This is the wrong syntax for doing this:
#IBOutlet weak var imageView: UIImageView! {
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}
The reason for the following error
#IBOutlet attribute requires property to be mutable
is.. having a closure after a property declaration acts as a getter. As an example I can do this
let two = 2
let three = 3
var five: Int {
return two + three
}
I cannot set any of the above values. two and three are let only (read only constants) and five is a computed property, so I can also only read that. I wont complicate the matter further but I would highly recommend giving this part of the documentation a read when you can.
Solution
You should have an outlet only:
#IBOutlet weak var imageView: UIImageView!
and then somewhere else in the code you can do things with it.
I think in your case it would be best on viewDidLoad. ViewDidLoad is called after the view and all of its outlets and properties have been set. So this is a safe place to start to use your outlets to further configure your view.
override func viewDidLoad() {
super.viewDidLoad()
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}
First of all, it's not good approach to override outlet's getter.
Secondly, technically you can override getter and setter of outlet.
In your case you defined getter, and forgot about setter. So you got get only variable.
private var _someView: UIImageView!
#IBOutlet weak var someView: UIImageView! {
get {
self.someView.contentMode = .scaleToFill
self.someView.clipsToBounds = true
return _someView
}
set {
_someView = newValue
}
}
You should use private variable to avoid recursion in getter.
Just try in this way and invoke this method inside viewDidLod()
func createRoundGreenImage(imageView : UIImageView, with image :
String) {
self.imageView.contentMode = .scaleToFill
self.imageView.clipsToBounds = true
}
I am trying to get the UIImageView wCircle to change to red when the UIImageView on a different viewController rDot is tapped. The problem is, when I tap rDot I get the error Thread 1: EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP subcode = 0x0) I made wCircle a global variable so it could be reached in the other viewController.
First viewController
weak var wCircle: UIImageView!
class SecondViewController: UIViewController {
#IBOutlet weak var wCircle: UIImageView!
}
Second viewController
class ProgressViewController: UIViewController {
#IBOutlet weak var rDot: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
rDot.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(rdotimageTapped(tapGestureRecognizer:)))
rDot.addGestureRecognizer(tapGestureRecognizer)
}
func rdotimageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
wCircle.image = wCircle.image!.withRenderingMode(.alwaysTemplate) //error on this line
wCircle.tintColor = UIColor.red
}
}
you code is not organized and indicate nothing Any way
change this line of code
replace weak var wCircle: UIImageView!
with
weak var wCircle = UIImageView()
and this
func rdotimageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
wCircle?.image = whiteDot.image!.withRenderingMode(.alwaysTemplate) //error on this line
wCircle?.tintColor = UIColor.red
}
wCircle is weak variable and it is optional value.So when you are calling it from other class it can't be optional. Also make it strong variable
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var userGuessTextField: UITextField!
#IBOutlet var resultLabel: UIView!
#IBAction func Guess(sender: AnyObject) {
let diceRoll = String(arc4random_uniform(6))
if diceRoll == userGuessTextField.text
{
resultLabel.text = "You are right"
}
else
{
resultLabel.text = "You are "
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Can anyone help me? Why I am getting this error as I am a beginner to iOS and learning this language.
please change...
#IBOutlet var resultLabel: UIView!
to
#IBOutlet var resultLabel: UILabel!
You are setting outlet like #IBOutlet var resultLabel: UIView!, It is type of UIView, so your resultLabel object is of UIView and it's not have property text
I think you should take outlet of UILabel instead of UIView.
So, drag a label in interface builder - ctrl + drag from it to class (in assistant editor) - and give outlet name.
Hope this will help :)
From Apple documents:
The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area.
... More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabel object draws a text string and a UIImageView object draws an image.
Remove the resultLabel from your storyboard/xib (delete the UIView and drag a new UILabel ) and from code change:
from:
#IBOutlet var resultLabel: UIView!
to
#IBOutlet var resultLabel: UILabel!
Then connect your new UILabel to the IBOutlet writed on code.
I am using Xcode 6 with swift.
I am trying to pass an image / UIImage from my UITabBarController to one of the subviews (tabs). The Image itself is being passed to the UITabBarController using the loadImage function (which is in return called by another UIViewController during prepareForSegue).
//this is the tab bar controller
class DesignerTabBarController : UITabBarController{
var pickedImage : UIImage = UIImage() //empty image
var VC1: DesignerTabController = DesignerTabController.alloc()
func loadImage(passedImage: UIImage){
pickedImage = passedImage
println(pickedImage) // image is accessible from here
}
override func viewDidLoad() {
super.viewDidLoad()
println(pickedImage) //image is accessible from here
VC1.pickedImageView.image = pickedImage // this line gives "unexpectedly found nil when unwrapping optional"
}
}
//this is the `UIViewcontroller` I want display the image (inside pickedImageView)
class DesignerTabController : UIViewController{
#IBOutlet weak var pickedImageView: UIImageView!
}
I realize that my way of passing the image from the UITabBarController to the sub-view will not work, even though when there are no syntax errors displayed within Xcode (it suggested pickedImageView when I started typing so I think I am really close to the solution).
If it's not possible passing data from my UITabBarController to the (first) subview, how can I pass data directly to the first subview? As I explained above, I am passing the image from another view using prepareForSegue. Does prepareForSegue directly to the first view work in my case?
Thank you very much for any suggestions, I'll try them and get back with my findings.
Your tab bar controller has viewControllers property. Just iterate through it check which controller is kind of class you need.
func passImage(passedImage: UIImage)
{
if viewControllers!.isEmpty{
return
}
for index in 0...viewControllers!.count{
if let controller = viewControllers[index] as? DesignerTabController
controller.pickedImageView.image = passedImage
}
}
solved by using viewDidLayoutSubviews() instead of viewDidLoad()
see below code for my working solution:
class DesignerTabController : UIViewController{
#IBOutlet weak var pickedImageView: UIImageView!
#IBOutlet weak var labelTest: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
class DesignerTabBarController : UITabBarController{
var pickedImage : UIImage = UIImage()
func loadImage(passedImage: UIImage){
pickedImage = passedImage
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
if let controller = viewControllers![0] as? DesignerTabController{
controller.pickedImageView.image = pickedImage
}
}
}