2016-12-04 81 views
0

下面的代碼會在uiimageview上拍攝圖片和顯示。 pitcure可以填滿整個視野。如何使圖像顯得更小,以便保存的圖像不是全部屏幕。我希望在那裏拍攝照片的邊框。一個矩形邊框。謝謝。如何更改圖像選取器控制器中的圖像大小

@IBAction func Camera(_ sender: AnyObject) { 

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){ 
     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.camera; 
     imagePicker.allowsEditing = false 
     self.present(imagePicker, animated: true, completion: nil)}} 

      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject: AnyObject]!){ 
       cameraScreen.image = image 
       self.dismiss(animated: true, completion: nil); 
       screenView = UIImageView() 

       screenView.frame = CGRect(x:0, y: 0, width: self.view.frame.width, height: self.view.frame.height) 





       UIGraphicsBeginImageContext(self.cameraScreen.frame.size) 
       self.view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true) 



} 
+0

你想擴展您從圖像拾取完成處理得到的圖像? –

回答

1

這是您必須在didFinishPickingImage完成處理程序中調用的一種方法。

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 
} 

此方法將改變圖像的大小。

+0

CGSize讓它在Swift 3中不可用 –

+0

如何編寫didFinishPickingImage完成處理程序?這是準確的func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo info:[String:AnyObject]){ 如果讓pickImage = info [UIImagePickerControllerOriginalImage]爲? UIImage dismissViewControllerAnimated(true,completion:{self.customFunction(pickedImage)}) } else { dismissViewControllerAnimated(true,completion:nil) } }。 –

0

更新基爾提Parghi的回答斯威夫特3

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

    let scale = newWidth/image.size.width 
    let newHeight = image.size.height * scale 
    UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) 
    image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
}