![](/img/trans.png)
[英]UIImagePickerController with UIPopOverController, iPad ios 6
[英]UIImagePickerController on iPad with IOS9
從iOS 9和Xcode 7開始,我無法再在iPad(設備和模擬器)上實現UIImagePickerController。 以下代碼適用於iPad,但僅適用於iOS 9.使用iOS 9+時,顯示的圖像(在UIImagePickerController被取消后)是所選圖像的錯誤版本。 沒有重新調整大小或裁剪最終圖像只是原始圖像的右上角?? 另外一個問題 - 如果imagePicker.allowsEditing = false,你無法從PhotoLibrary中選擇圖像?
@IBAction func photoButton(sender: AnyObject) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: false, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
self.imageView.image = pickedImage
dismissViewControllerAnimated(true, completion: { () -> Void in
})
}
下面是UIImagePickerController中顯示的選定圖像的示例。 (注意所選圖像如何呈現非常小而不像以前那樣呈現屏幕的全尺寸/寬度)
在UIImagePickerController中選擇使用按鈕后,最終圖像僅在原始圖像的右上角。 我在做錯了什么或是在iOS 9上破壞了UIImagePickerController?
這是Apple的一個錯誤: http : //openradar.appspot.com/radar?id = 5032957332946944
當前糟糕的解決方法:
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
imagePicker.allowsEditing = false
} else {
imagePicker.allowsEditing = true
}
Swift 3.0:
if UIDevice.current.userInterfaceIdiom == .pad {
}
請參閱以下編碼
@IBAction func photoButton(sender: AnyObject)
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
picker!.sourceType = UIImagePickerControllerSourceType.Camera
self .presentViewController(picker!, animated: true, completion: nil)
}
else
{
let alertWarning = UIAlertView(title:"Warning", message: "You don't have camera", delegate:nil, cancelButtonTitle:"OK", otherButtonTitles:"")
alertWarning.show()
}
}
func openGallary()
{
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(picker!, animated: true, completion: nil)
}
//PickerView Delegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
println("picker cancel.")
}
你需要確保你的imageview有正確的框架,你的imageview的內容模式應該是aspectfit,而選擇器返回的圖像是[UIImagePickerControllerOriginalImage]
類型
[imageView setFrame:imageViewRect];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
迅速:
imageView.setFrame(imageViewRect)
imageView.setContentMode(UIViewContentModeScaleAspectFit)
並且來到allowsEditing
屬性,它與照片選擇無關。
它是一個布爾值,指示是否允許用戶編輯選定的靜止圖像或電影。
如果要從相機庫中選擇照片,則需要將源類型修改為
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
更新:
LEts表示您已選擇一個圖像並將其顯示在圖像視圖上。 我假設您在視圖上有一個imageview,並且imageview框架等於視圖框架。 如果IB是自由形式的,我假設IB中的imageview幀的大小為600x600。
如果你只是這樣做:
_startImageView.image=_img;
結果將是:
現在讓我們對要在imageview中顯示的圖像進行一些更改:
CGRect scaledRect = AVMakeRectWithAspectRatioInsideRect(_img.size, CGRectMake(0, 0, self.startImageView.frame.size.width, self.startImageView.frame.size.height));
UIGraphicsBeginImageContext(CGSizeMake(_startImageView.frame.size.width,_startImageView.frame.size.height));
[_img drawInRect:scaledRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_startImageView.image=scaledImage;
現在圖像將是:
選擇的原始圖像大小為640x426, 未縮放時 。
縮放到最大時選擇的原始圖像大小為1536x1286 (使用兩個手指縮放動作) 。
正如您所看到的,圖像中仍然沒有太大的變化, 這是因為在您的imageview收到圖像時圖像已被裁剪/縮放 !
所以即使你試着這樣做:
[_img drawInRect:_startImageView.frame];
根據我們的需要不會繪制圖像,因為圖像已經在它們的右側縮放,因為拾取器給出的圖像是edited image
。
解:
要解決此問題,您需要在didFinishPickingMediaWithInfo:
方法中從選擇器中選擇原始圖像
info[UIImagePickerControllerOriginalImage];
它給你的形象:
這段代碼與@ pkc456相似,只是稍微短一些。
這對我很有用:
import UIKit
class PostImageViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet var imageToPost: UIImageView!
@IBAction func chooseImage(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false //or true additional setup required.
self.presentViewController(image, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion:nil)
}
這有用嗎?
我正在研究xcode 7.1(Swift)並發現你的代碼合適。 我還在我的項目中編寫了以下代碼,並且它正在成功運行。
func showPicker(){
let type = UIImagePickerControllerSourceType.PhotoLibrary
if(UIImagePickerController.isSourceTypeAvailable(type)){
let pickerController = UIImagePickerController()
pickerController.delegate = self
pickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
pickerController.allowsEditing = false
self.presentViewController(pickerController, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let imageView = self.view.viewWithTag(20) as! UIImageView
let selectedImage : UIImage = image
imageView.image=selectedImage
self.dismissViewControllerAnimated(true, completion: nil)
}
代碼和代碼的唯一區別在於ImagePickerController
實例的可見性。 供您參考,我上傳了我的代碼: - https://www.dropbox.com/s/pe0yikxsab8u848/ImagePicker-Swift.zip?dl=0
我的想法只是查看我的代碼一次,也許您會了解代碼的哪一部分出現故障。
您似乎正在使用IB和自動布局,但已設置了正確的約束。 嘗試在故事板中添加一些約束。
對我來說,我解決了,顯示模式為popover
@IBAction func photoButton(sender: AnyObject) {
imagePicker.allowsEditing = true
imagePicker.sourceType = .PhotoLibrary
let controller = self.imagePicker
controller.modalPresentationStyle = UIModalPresentationStyle.Popover
controller.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
let popover = controller.popoverPresentationController
popover?.sourceView = self
controller.preferredContentSize = CGSize(
width: self.frame.width * 0.6,
height: self.frame.height * 0.6
)
popover?.sourceRect = CGRectMake(
CGRectGetMidX(self.bounds),
CGRectGetMidY(self.bounds),
0,
0
)
self.presentViewController(self.imagePicker, animated: true, completion: nil)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.