2016-09-23 9 views
0

카메라가 신속하게 사진을 찍은 후에 사진의 크기를 조정하는 데 문제가 있습니다. 현재 내 장면의 이미지보기가 100 x 100 (프로필 사진 용)이지만 앱을 실행하면 카메라를 사용한 후에도 사진이 나타나지 않습니다. 여기 내 코드가있다. 내가 실종 무엇 :UIImagePicker/Swift로 사진 크기를 조절하는 방법

@IBAction func employeePhotoButton(sender: AnyObject) { 

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) { 
     var imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.Camera; 
     imagePicker.allowsEditing = false 
     self.presentViewController(imagePicker, animated: true, completion: nil) 

     func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage { 

      let scale = newWidth/image.size.width 
      let newHeight = image.size.height * scale 
      UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)) 
      image.drawInRect(CGRectMake(0, 0, newWidth, newHeight)) 
      let newImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext() 

      return newImage 
     } 

     func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) { 

      employeePhoto.image = resizeImage(image!, newWidth: 100) 

      self.dismissViewControllerAnimated(true, completion: nil); 
     } 


    } 
} 

답변

2

(이 사용 Swift3 구문을 참고) 같은 것을보십시오 :

// your IBAction could look something like this 
@IBAction func employeePhotoButton(sender: AnyObject) 
{ 
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) 
    { 
     showImagePicker(.camera) 
    } 
    else 
    { 
     print("camera not available") 
    } 
} 

// your function in your view controller to display the image picker with whichever 
// source type you want, check for source type availability before calling 
func showImagePicker(sourceType:UIImagePickerControllerSourceType) 
{ 
    // instantiates and configures class var 
    self.imagePickerController = UIImagePickerController.init() 
    self.imagePickerController?.modalPresentationStyle = .currentContext 
    self.imagePickerController?.sourceType = sourceType 
    self.imagePickerController?.delegate = self 
    self.imagePickerController?.allowsEditing = false 

    // present controller 
    self.present(self.imagePickerController!, animated: true, completion: nil) 
} 


// Image picker delegate function 
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) 
{ 
    // dismiss the picker view controller 
    print("Selected media with info: \(info)") 
    self.dismiss(animated: true, completion: nil) 

    // no longer need this so nil out now 
    self.imagePickerController = nil 

    // check our image is valid 
    guard (info[UIImagePickerControllerOriginalImage] != nil) else { 
     print("No image found to load...") 
     return 
    } 

    // sets our 'selectedImg' class var for use elsewhere if needed 
    selectedImg = info[UIImagePickerControllerOriginalImage] as? UIImage 

    // verify the image is not nil 
    guard selectedImg != nil else { 
     print("selected image is nil") 
     return 
    } 

    // resize our selected image 
    let resizedImage = selectedImg.convert(toSize:CGSize(width:100.0, height:100.0), scale: UIScreen.main.scale) 

    // TODO: whatever you want to do with your resized image 
    employeePhoto.image = resizedImage 
} 


extension UIImage 
{ 
    // convenience function in UIImage extension to resize a given image 
    func convert(toSize size:CGSize, scale:CGFloat) ->UIImage 
    { 
     let imgRect = CGRect(origin: CGPoint(x:0.0, y:0.0), size: size) 
     UIGraphicsBeginImageContextWithOptions(size, false, scale) 
     self.draw(in: imgRect) 
     let copied = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 

     return copied! 
    } 
} 
+0

감사합니다! 귀하의 답변을 지침으로 사용하여 사진의 크기를 적절하게 조정할 수있었습니다. 내 유일한 관심사는 사진이 약간 흐릿하다는 것입니다. 그 문제를 해결할 수 있는지 확실하지 않지만 다른 문제를 만들 것입니다. 다시 한 번 감사드립니다! –