![](/img/trans.png)
[英]UIImagePickerController allowsEditing = YES not working
[英]UIImagePickerController AllowsEditing not working
我正在使用 UIImagePickerController 來允許用戶拍照。 我想讓他/她事后編輯它,但是,無論我做什么,我都一無所獲。
這是我的代碼(我使用的是 Xamarin):
UIImagePickerController imagePicker = new UIImagePickerController ();
// set our source to the camera
imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
// set what media types
//imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes (UIImagePickerControllerSourceType.Camera);
// show the camera controls
imagePicker.ModalPresentationStyle = UIModalPresentationStyle.CurrentContext;
imagePicker.ShowsCameraControls = true;
imagePicker.AllowsEditing = true;
imagePicker.SetEditing (true,true);
imagePicker.PreferredContentSize = new SizeF(900,600);
imagePicker.CameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Photo;
imagePicker.Title="taste.eat. image";
// attach the delegate
imagePicker.Delegate = new ImagePickerDelegate();
// show the picker
NavigationController.PresentViewController(imagePicker, true,null);
我錯過了什么嗎?
編輯:
我已經按照教程操作,我正在使用矩形進入屏幕,但是如果我平移或縮放它,一旦我抬起手指,它就會回到中心。 是否可以從照片應用程序進入此屏幕?
當使用UIImagePickerController's delegate
方法 - imagePickerController:didFinishPickingMediaWithInfo:
,我們使用
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
此代碼將始終返回原始圖像,即使編輯處於開啟狀態。
嘗試使用
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
如果編輯打開,這將返回編輯后的圖像。
希望這可以幫助。
AllowsEditing
屬性僅允許用戶在選擇圖像時裁剪為正方形,並在選擇視頻時修剪視頻。
任何其他功能都需要使用自定義 UI 和代碼來實現。
請參閱此問題: iPhone SDK - 如何在啟用允許編輯的情況下自定義 UIImagePickerController 中的裁剪矩形?
不幸的是,您在屏幕截圖中顯示的內容不是UIImagePickerController
一部分
斯威夫特 3
我很難返回裁剪后的圖像(我的最后一個簡單的錯誤)。 而不是使用 UIImagePickerControllerOriginalImage,你需要 UIImagePickerControllerEditedImage。 見下文:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
// The info dictionary contains multiple representations of the image, and this uses the cropped image.
let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage
// Set yourImageView to display the selected image.
yourImage.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
斯威夫特 4+
Swift 4 之后發生了一些變化。 UIImagePickerControllerEditedImage
更改為UIImagePickerController.InfoKey.editedImage
。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let selectedImage = info[UIImagePickerController.InfoKey.editedImage] as! UIImage
imageView.image = selectedImage
dismiss(animated: true, completion: nil)
}
無法僅通過更改allowsEditing = YES
類的屬性來enable filters
。 它只會顯示裁剪工具。 根據您的屏幕截圖,您似乎集成了一些有問題的開源庫,如果不查看源代碼,則很難修復中心裁剪錯誤。
最好發布一些有關您的實現的具體細節或切換到標准的開源庫。
它會像這樣工作。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let selectedImage = info[UIImagePickerControllerEditedImage] as! UIImage
userPhoto.image = selectedImage
dismiss(animated: true, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.