Swift 调整图片大小

Swift resize Image

我找到了下面的代码来调整图像的大小,但是我在使用它的时候出错了。它说

fatal error: unexpectedly found nil while unwrapping an Optional value

我找不到错误的原因。

调整图像大小函数:

func ResizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
        let size = image.size
        let widthRatio  = targetSize.width  / image.size.width
        let heightRatio = targetSize.height / image.size.height

        // Figure out what our orientation is, and use that to form the rectangle
        var newSize: CGSize
        if(widthRatio > heightRatio) {
            newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
        } else {
            newSize = CGSizeMake(size.width * widthRatio,  size.height * widthRatio)
        }

        // This is the rect that we've calculated out and this is what is actually used below
        let rect = CGRectMake(0, 0, newSize.width, newSize.height)

        // Actually do the resizing to the rect using the ImageContext stuff
        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
        image.drawInRect(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }

我的 ImagePickerController:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
    {

        let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage

        if flag == 1 {
            //self.ResizeImage(UIImage(named: "\(pickedImage)")!, targetSize: CGSizeMake(200.0, 200.0))
            leftImage.image = self.ResizeImage(UIImage(named: "pickedImage")!, targetSize: CGSizeMake(750.0, 750.0)) //pickedImage
            let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
            let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
            photo1 = base64String

        }else if flag == 2 {
            rightImage.image = self.ResizeImage(UIImage(named: "\(pickedImage)")!, targetSize: CGSizeMake(750.0, 750.0)) //pickedImage
            let imageData = UIImageJPEGRepresentation(pickedImage!, 0.5)
            let base64String = imageData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
            photo2 = base64String
        }

        dismissViewControllerAnimated(true, completion: nil)



    }
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)!
    }

这是因为 UIImage class 没有任何成员大小 使用以下代码运行 this

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

let size = image?.size ?? CGSizeZero

引用自link