我正在处理我的第一个xamarin ios应用程序,该应用程序在UITabBarController中托管的UICollectionView中显示图像。初始视图是没有TabBarController的普通视图。初始视图上的按钮加载一个新视图(UITabBarController),该视图在UICollectionView中显示图像。我们工作得很好,然后我将Mac OS更新到Sierra,XCode更新到版本8.3.2,每次当我从初始控制器中断到显示UICollectionView中的图像的(UITabBarController)视图时,应用程序就开始崩溃。
以下是我在应用程序输出窗口中获得的异常
2017-06-06 19:32:50.958 MyIOSApp [6085:1639375]批评:Stacktrace:
2017-06-06 19:32:50.958 MyIOSApp [6085:1639375]批评:at < 0xffffffff的> 2017-06-06 19:32:50.958 MyIOSApp [6085:1639375] critical:at(包装器托管到原生) Foundation.NSObject.xamarin_release_managed_ref (intptr,Foundation.NSObject)< 0x00007> 2017-06-06 19:32:50.958 MyIOSApp [6085:1639375]批评:at Foundation.NSObject.ReleaseManagedRef()[0x00000] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/Foundation/NSObject2.cs:208 2017-06-06 19:32:50.959 MyIOSApp [6085:1639375]批评:at Foundation.NSObject / NSObject_Disposer.Drain(Foundation.NSObject) [0x00062] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/Foundation/NSObject2.cs:784 2017-06-06 19:32:50.959 MyIOSApp [6085:1639375]批评:at(包装器 runtime-invoke)object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr)[0x0001e] in < 2f4074c3120b4d80802e10af84b67d41#2892ED79-E936-4F04-126B-3B9FB0CB5554>:0 2017-06-06 19:32:50.959 MyIOSApp [6085:1639375]批评:at < 0xffffffff的> 2017-06-06 19:32:50.959 MyIOSApp [6085:1639375] critical:at(包装器托管到原生) UIKit.UIApplication.UIApplicationMain(int,string [],intptr,intptr) < 0x00007> 2017-06-06 19:32:50.959 MyIOSApp [6085:1639375]批评:
在UIKit.UIApplication.Main(string [],intptr,intptr)[0x00005] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:79 2017-06-06 19:32:50.959 MyIOSApp [6085:1639375]批评:at UIKit.UIApplication.Main(string [],string,string)[0x00038] in /Users/builder/data/lanes/4691/d2270eec/source/xamarin-macios/src/UIKit/UIApplication.cs:63 2017-06-06 19:32:50.959 MyIOSApp [6085:1639375]批评:at MyIOSApp.Application.Main(string [])[0x00001] in /Users/ETI-MAC3/Documents/Apps/p9Tech/Xamarin/iOSApp/MyIOSApp/MyIOSApp/Main.cs:12 2017-06-06 19:32:50.960 MyIOSApp [6085:1639375]批评:at(包装器 runtime-invoke)object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr)[0x0001e] in < 2f4074c3120b4d80802e10af84b67d41#2892ED79-E936-4F04-126B-3B9FB0CB5554>:0 2017-06-06 19:32:50.960 MyIOSApp [6085:1639375]批评:原生 堆栈跟踪:2017-06-06 19:32:51.014 MyIOSApp [6085:1639375]关键:0
使用的本机库之一
libmonosgen-2.0.dylib 0x000000010400d920 mono_handle_native_crash + 260 2017-06-06 19:32:51.014 MyIOSApp [6085:1639375]批评:1 libmonosgen-2.0.dylib
0x00000001040193d4 mono_sigsegv_signal_handler + 316 2017-06-06 19:32:51.014 MyIOSApp [6085:1639375]批评:2
libsystem_platform.dylib 0x000000018509b348 _sigtramp + 52 2017-06-06 19:32:51.014 MyIOSApp [6085:1639375]批评:3
libxamarin-debug.dylib 0x0000000104318b94 xamarin_release_managed_ref + 208 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]关键:4 libXamarin.iOS.dll.dylib
0x00000001023ba968 wrapper_managed_to_native_Foundation_NSObject_xamarin_release_managed_ref_intptr_Foundation_NSObject +10 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]关键:5 libXamarin.iOS.dll.dylib 0x00000001023bad48 Foundation_NSObject_ReleaseManagedRef + 24 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]关键:6 libXamarin.iOS.dll.dylib
0x00000001023bdf88 Foundation_NSObject_NSObject_Disposer_Drain_Foundation_NSObject + 312 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]批评:7
libmscorlib.dll.dylib 0x00000001004ef574 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]关键:8 libmonosgen-2.0.dylib 0x000000010401c4fc mono_jit_runtime_invoke + 836 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]关键:9 libmonosgen-2.0.dylib
0x0000000104096fe4 do_runtime_invoke + 112 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]关键:10 libmonosgen-2.0.dylib
0x0000000104096f40 mono_runtime_invoke + 208 2017-06-06 19:32:51.015 MyIOSApp [6085:1639375]关键:11 MyIOSApp
0x000000010005eebc _ZL32native_to_managed_trampoline_112P11objc_objectP13objc_selectorPP11_MonoMethodP8NSObjectj + 376 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]关键:12 MyIOSApp 0x000000010005ed38 + [ NSObject_Disposer drain:] + 60 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]关键:13基金会 0x0000000186af26ec + 340 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]批评:14 CoreFoundation
0x0000000185fba278 + 24 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]批评:15 CoreFoundation
0x0000000185fb9bc0 + 524 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]批评:16 CoreFoundation
0x0000000185fb77c0 + 804 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]批评:17 CoreFoundation
0x0000000185ee6048 CFRunLoopRunSpecific + 444 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]关键:18个GraphicsServices
0x0000000187969198 GSEventRunModal + 180 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]批评:19 UIKit
0x000000018bec0628 + 684 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]批评:20 UIKit
0x000000018bebb360 UIApplicationMain + 208 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]关键:21 libXamarin.iOS.dll.dylib
0x0000000102430d34 wrapper_managed_to_native_UIKit_UIApplication_UIApplicationMain_int_string___intptr_intptr + 340 2017-06-06 19:32:51.016 MyIOSApp [6085:1639375]关键:22 libXamarin.iOS.dll.dylib 0x00000001023d3b0c UIKit_UIApplication_Main_string___intptr_intptr + 44 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]批评:23 libXamarin.iOS.dll.dylib 0x00000001023d3acc UIKit_UIApplication_Main_string___string_string + 172 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]批评:24 libMyIOSApp.exe.dylib 0x0000000100145330 MyIOSApp_Application_Main_string + 128 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]关键:25 libmscorlib.dll.dylib
0x00000001004ef574 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 244 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]关键:26 libmonosgen-2.0.dylib 0x000000010401c4fc mono_jit_runtime_invoke + 836 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]批评:27 libmonosgen-2.0.dylib
0x0000000104096fe4 do_runtime_invoke + 112 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]批评:28 libmonosgen-2.0.dylib
0x000000010409a208 do_exec_main_checked + 152 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]批评:29 libmonosgen-2.0.dylib
0x0000000104000134 mono_jit_exec + 268 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]关键:30 libxamarin-debug.dylib
0x0000000104325804 xamarin_main + 2148 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]关键:31 MyIOSApp
0x0000000100075454 main + 96 2017-06-06 19:32:51.017 MyIOSApp [6085:1639375]关键:32 libdyld.dylib
0x0000000184ec85b8 + 4 2017-06-06 19:32:51.018 MyIOSApp [6085:1639375]至关重要: ================================================== ===============执行本机代码时获得了SIGSEGV。这通常表示致命 单声道运行时中的错误或的应用程序。
我上传了符号化文件here。您可以验证并告诉我导致此问题的原因。
更新:
以下是主视图的准备seque代码:
public override void PrepareForSegue(UIStoryboardSegue segue,NSObject sender) { base.PrepareForSegue(segue,sender);
List<UIViewController> visibleViewControllers = new List<UIViewController>();
try
{
var tabBarViewController = segue.DestinationViewController as UITabBarController;
var similarImagesViewController = tabBarViewController.ViewControllers[0] as SimilarImagesViewController;
similarImagesViewController.SimilarImages = myIOSApp.SimilarImages;
if (myIOSApp.SimilarImages.Count > 0)
{
visibleViewControllers.Add(similarImagesViewController);
}
if (!string.IsNullOrEmpty(myIOSApp.WikiLink))
{
var wikiViewController = tabBarViewController.ViewControllers[1] as WikiViewController;
wikiViewController.WikiLink = myIOSApp.WikiLink;
visibleViewControllers.Add(wikiViewController);
}
((TabBarViewController)tabBarViewController).VisibleViewControllers = visibleViewControllers;
}
catch (Exception ex)
{
AlertViewController.PresentOKAlert("My IOS App", ex.Message, this);
}
}
以下是TabBarViewController类的代码:
public partial class TabBarViewController : UITabBarController
{
public List<UIViewController> VisibleViewControllers
{
get;
set;
}
public TabBarViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
SetViewControllers(VisibleViewControllers.ToArray(), true);
}
}
以下是SimilarImagesViewController的代码
public partial class SimilarImagesViewController : BaseUIViewController
{
private static SimilarImagesSource _SimilarImagesSource;
public List<Models.Image> SimilarImages
{
get;
set;
}
public SimilarImagesViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
try
{
Title = "Similar Images";
_SimilarImagesSource = new SimilarImagesSource(SimilarImages);
collView.Source = _SimilarImagesSource;
InvokeOnMainThread(() =>
collView.ReloadData()
);
}
catch (Exception ex)
{
AlertViewController.PresentOKAlert("MyIOSApp", ex.Message, this);
}
}
}
以下是UICollectionView源代码的源代码
public class SimilarImagesSource : UICollectionViewSource, IUICollectionViewDelegateFlowLayout
{
public SimilarImagesSource(List<Image> similarImages)
{
Rows = similarImages;
}
public List<Image> Rows { get; private set; }
public override nint NumberOfSections(UICollectionView collectionView)
{
return 1;
}
public override nint GetItemsCount(UICollectionView collectionView, nint section)
{
return Rows.Count;
}
[Export("collectionView:layout:sizeForItemAtIndexPath:")]
public CoreGraphics.CGSize GetSizeForItem(UICollectionView collectionView, UICollectionViewLayout layout, NSIndexPath indexPath)
{
UIImage image = GetUIImageFromBase64String(Rows[indexPath.Row].ImageString);
var cgSize = new CoreGraphics.CGSize(image.Size.Width, image.Size.Height);
image.Dispose();
return cgSize;
}
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
var cell = (ImageCell)collectionView.DequeueReusableCell(ImageCell.Id, indexPath);
cell.Layer.BorderColor = UIColor.White.CGColor;
cell.BackgroundColor = UIColor.White;
var imageView = new UIImageView(GetUIImageFromBase64String(Rows[indexPath.Row].ImageString));
cell.AddSubview (imageView);
return cell;
}
private UIImage GetUIImageFromBase64String(string imageString)
{
if (imageString.StartsWith("http", StringComparison.CurrentCulture))
{
return LoadImage(imageString);
}
var imageBytes = Convert.FromBase64String(imageString);
var imageData = NSData.FromArray(imageBytes);
var uiImage = UIImage.LoadFromData(imageData);
return uiImage;
}
public UIImage LoadImage(string imageUrl)
{
byte[] contents;
using(var httpClient = new HttpClient())
{
Task<byte[]> contentsTask = httpClient.GetByteArrayAsync(imageUrl);
// await! control returns to the caller and the task continues to run on another thread
contents = contentsTask.Result;
}
// load from bytes
return UIImage.LoadFromData(NSData.FromArray(contents));
}
}
谢谢, 与Hemant。