In Xcode 6.1. 'Uiimage' Does Not Have a Member Named 'Size' Error

In Xcode 6.1. 'UIImage?' does not have a member named 'size' Error

That initializer is now a failable initializer, and as such it returns an optional UIImage.

To quickly fix your bug just unwrap the image:

let sizeOfImage = image?.size

but I presume you will reference the image variable more than just once in your code, in that case I suggest using optional binding:

if let image = image {
let sizeOfImage = image.size
/// ...
/// Use the image
}

NS objects don't have recognized members

Your icon variable is optional due to NSImage(named... possibly not returning an image due to missing image file or mistyped name, etc.

So to access the members you need to unwrap icon like

icon!.size = NSSize(width: 16, height: 16)
icon!.setTemplate(true)

...although you should test that icon is not nil before you unwrap it. e.g.

if let icn = icon {
icn.size = NSSize(width: 16, height: 16)
icn.setTemplate(true)
}

Swift Xcode 6.1 broke my code | Operand error

In this line:

let image = UIImage(named: imageNamed)

image is an optional, UIImage?.

The error on this line:

let result = SKScrollingNode(color: UIColor.clearColor(), size: CGSizeMake(CGFloat(containerWidth), image.size.height));

can be narrowed down to this piece of code:

CGSizeMake(CGFloat(containerWidth), image.size.height)

CGSizeMake requires 2 CGFloat parameters, but the 2nd one is an optional, because image is an optional:

  • if image is not nil, image.size.height evaluates to a CGFloat
  • if image is nil, image.size.height evaluates to nil

In order to avoid that, you have 2 options:

  1. make image a non optional by using forced unwrapping

    let image = UIImage(named: imageNamed)!

    but I do not recommend using it because if the UIImage creation fails, the app will crash.

  2. Use optional binding:

    let image = UIImage(named: imageNamed);
    if let image = image {
    let result = SKScrollingNode(color: UIColor.clearColor(), size: CGSizeMake(CGFloat(containerWidth), image.size.height));
    // ... rest of the code here
    }

    this is a better way to unwrap the optional, because in case it's nil, the code in the if statement will be skipped and no runtime error will occur.

Swift - Checking size of UIImageView

Before we get to your question, several points:

Don't start the name of an instance variable with upper case.

Don't use "image" for the variable name of an image view. That suggests that it is a UIImage. It's a UIImageView, not a UIImage. Call it something like theImageView.

Now to your question: UIView objects do not have a size property (UIImage objects do have a size property. That might be the cause of your confusion.) They have a bounds rectangle and a frame rectangle. You can use theImageView.bounds.size or theImageView.frame.size.

Swift resize Image

func resizeImage(image:UIImage) -> UIImage
{
var actualHeight:Float = Float(image.size.height)
var actualWidth:Float = Float(image.size.width)

let maxHeight:Float = 180.0 //your choose height
let maxWidth:Float = 180.0 //your choose width

var imgRatio:Float = actualWidth/actualHeight
let maxRatio:Float = maxWidth/maxHeight

if (actualHeight > maxHeight) || (actualWidth > maxWidth)
{
if(imgRatio < maxRatio)
{
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
}
else if(imgRatio > maxRatio)
{
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
}
else
{
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}

let rect:CGRect = CGRectMake(0.0, 0.0, CGFloat(actualWidth) , CGFloat(actualHeight) )
UIGraphicsBeginImageContext(rect.size)
image.drawInRect(rect)

let img:UIImage = UIGraphicsGetImageFromCurrentImageContext()
let imageData:NSData = UIImageJPEGRepresentation(img, 1.0)!
UIGraphicsEndImageContext()

return UIImage(data: imageData)!
}

How to add image in a xib view controller (swift3)

Assuming your UIImageView is named imageView and that your image is named "MyImage.jpg", you do this:

let theImage = UIImage(named: "MyImage.jpg") // create a UIImage
imageView.image = theImage // UIImageView takes a UIImage

Also note, UIImage(named:) is (1) case-sensitive and will result in a null image if not found, and (2) if your image is a PNG you do not need the file type.

One last note, if you misspell the image file and UIImage is null? The error will happen on the second line of code, not the first.



Related Topics



Leave a reply



Submit