2016-04-25 4 views
1

Ich habe den folgenden Code gefunden, um die Größe des Bildes zu ändern, aber ich bekomme Fehler bei der Verwendung. Es sagtSwift Größe ändern Image

fatal error: unexpectedly found nil while unwrapping an Optional value

Ich konnte den Grund des Fehlers nicht finden.

ResizeImage func:

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 
    } 

Mein 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) 



    } 

Antwort

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

Dies liegt daran, UIImage Klasse haben leider keinen beliebigen Mitglied Größe Verwenden Sie den folgenden Code, um dieses

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

OR

let size = image?.size ?? CGSizeZero 

refrenced von link