在什么情况下我会伤心?如果我将allowEditing
设置为false。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
// I am happy :)
} else {
// I am sad :(
}
dismiss(animated: true, completion: nil)
}
(我在iOS 11.2.1 iPhone SE
中遇到了崩溃(根据Crashlytics
),如果存在可能失败的合法条件或者只是iOS错误,那就很困惑。)
答案 0 :(得分:2)
在iOS 12中进行了更改
image.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
答案 1 :(得分:2)
方法签名已更改为:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}
// Set photoImageView to display the selected image.
photoImageView.image = selectedImage
// Dismiss the picker.
dismiss(animated: true, completion: nil)
}
请注意第二个参数:
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]
它不是前面方法签名中的字符串数组。
答案 2 :(得分:1)
我最终使用了这个:
SELECT listingsdbelements_field_value
FROM default_en_listingsdbelements
ORDER BY CAST(listingsdbelements_field_value AS INT) ASC
这样 -
import Photos
extension UIImage {
static func from(info: [String : Any]) -> UIImage? {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
return image
}
var imageToBeReturned: UIImage?
if let url = info[UIImagePickerControllerReferenceURL] as? URL,
let asset = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil).firstObject {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: CGSize(width: 1000, height: 1000), contentMode: .aspectFit, options: option, resultHandler: {(image: UIImage?, info: [AnyHashable : Any]?) in
imageToBeReturned = image
})
}
return imageToBeReturned
}
}
这对我有用,请建议任何改进:)
答案 3 :(得分:0)
我已经在Apple开发者论坛帖子中看到,如果iOS 11 UIImagePickerControllerOriginalImage
上的图片尺寸较大(超过2048 X 2048),则返回nil
。作为一个解决方法,帖子建议使用Photos framework来获取所选图像。该帖子提供的解决方案如下:
__block UIImage *image = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
if (@available(iOS 11.0, *))
{
PHAsset * asset = (PHAsset*)[info objectForKey:UIImagePickerControllerPHAsset];
PHImageManager *manager = [PHImageManager defaultManager];
PHImageRequestOptions *requestOptions = [[PHImageRequestOptions alloc] init];
requestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
requestOptions.synchronous = true;
[manager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:requestOptions resultHandler:^void(UIImage *img, NSDictionary *info) {
if(img != nil)
{
image = img;
}
}];
}
参考:iOS 11 does not return the original image for large images with UIImagePickerControllerOriginalImage
var image = info[UIImagePickerControllerOriginalImage]
if #available(iOS 11.0, *)
{
let asset = info[UIImagePickerControllerPHAsset] as! PHAsset
let manager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.resizeMode = PHImageRequestOptionsResizeMode.exact
requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.highQualityFormat
requestOptions.isSynchronous = true
manager.requestImage(for: asset, targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.default, options: requestOptions, resultHandler: { (img, info) in
if img != nil
{
image = img
}
})
}