试图将聊天客户端从swift 4传递到swift 4.2,并使用选择器发现问题。
UIImagePickerControllerEditedImage // 不能为索引为'UIImagePickerController.InfoKey'的'[String:Any]'类型下标
int startX = -halfWidth + rect.Left;
int startY = -halfHeight + rect.Top;
int stopX = width - halfWidth - ( width - rect.Right );
int stopY = height - halfHeight - ( height - rect.Bottom );
答案 0 :(得分:14)
方法签名已更改为
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any])
您应该看到功能名称的警告消息
实例方法 几乎'imagePickerController(:didFinishPickingMediaWithInfo :)' 符合可选要求 协议的“ imagePickerController(:didFinishPickingMediaWithInfo :)” 'UIImagePickerControllerDelegate'
候选人的类型不匹配'(UIImagePickerController,[String: 任何])->()'
将'imagePickerController(_:didFinishPickingMediaWithInfo :)'移动到 另一个扩展,以使此警告静音
将'imagePickerController(_:didFinishPickingMediaWithInfo :)'设为私有 消除此警告
需求'imagePickerController(_:didFinishPickingMediaWithInfo :)' 在此处声明(UIKit.UIImagePickerControllerDelegate)
答案 1 :(得分:7)
在Swift 4.2中,imagePickerController委托方法有所改变。请检查以下代码。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
var selectedImage: UIImage?
if let editedImage = info[.editedImage] as? UIImage {
selectedImage = editedImage
self.profileImage.image = selectedImage!
picker.dismiss(animated: true, completion: nil)
} else if let originalImage = info[.originalImage] as? UIImage {
selectedImage = originalImage
self.profileImage.image = selectedImage!
picker.dismiss(animated: true, completion: nil)
}
}
这可以肯定地工作。
答案 2 :(得分:4)
喜欢
private func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
picker.dismiss(animated: true, completion: nil)
let image = info[UIImagePickerController.InfoKey.originalImage]! as! UIImage
...
}
答案 3 :(得分:1)
Swift4.2
//MARK:- ImagePicker Controller Delegate
//MARK:-
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let chosenImage = info[.originalImage] as? UIImage {
yourimage.contentMode = .scaleAspectFill
yourimage.image = chosenImage
} else{
print("Something went wrong")
}
}
答案 4 :(得分:1)
using System;
using Xamarin.Forms;
namespace Sample.Utils
{
/// <summary>
/// Allows setting Top,Left,Right,Bottom Margin independently from each other.
/// The default implementation does not allow that, even from code. This
/// attached property remembers the previously set margins and only modifies what you intend to modify.
/// </summary>
public static class Margin
{
public static readonly BindableProperty LeftProperty = BindableProperty.CreateAttached(
propertyName: "Left",
returnType: typeof(double),
declaringType: typeof(Margin),
propertyChanged: LeftPropertyChanged,
defaultValue: 0.0d);
private static void LeftPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
SetLeft(bindable, (double)newValue);
}
public static void SetLeft(BindableObject element, double value)
{
var view = element as View;
if (view != null)
{
Thickness currentMargin = (Xamarin.Forms.Thickness)view.GetValue(View.MarginProperty);
view.Margin = new Thickness(value, currentMargin.Top, currentMargin.Right, currentMargin.Bottom);
}
}
public static double GetLeft(BindableObject element)
{
View view = element as View;
if (view != null)
{
Thickness margin = (Xamarin.Forms.Thickness)view.GetValue(View.MarginProperty);
return margin.Left;
}
return (double)LeftProperty.DefaultValue;
}
}
}
答案 5 :(得分:0)
extension CameraViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let chosenImage = info[.originalImage] as? UIImage {
selectedImage = chosenImage
photo.image = chosenImage
}
dismiss(animated: true, completion: nil)
}
}
以上代码解决了警告,图片成功上传