【问题标题】:Swift resize Image快速调整图像大小
【发布时间】:2016-08-18 14:51:19
【问题描述】:

我找到了以下代码来调整图像大小,但在使用时出现错误。它说

致命错误:在展开可选值时意外发现 nil

我找不到错误原因。

调整图像大小函数:

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)



    }

【问题讨论】:

    标签: ios uiimage resize swift2


    【解决方案1】:
    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)!
        }
    

    【讨论】:

      【解决方案2】:

      这是因为 UIImage 类没有任何成员大小 使用下面的代码来运行这个

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

      let size = image?.size ?? CGSizeZero
      

      引用自link

      【讨论】:

        猜你喜欢
        • 2011-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 2020-12-07
        相关资源
        最近更新 更多