Book Image

Xamarin Blueprints

By : Michael Williams
Book Image

Xamarin Blueprints

By: Michael Williams

Overview of this book

Do you want to create powerful, efficient, and independent apps from scratch that will leverage the Xamarin framework and code with C#? Well, look no further; you’ve come to the right place! This is a learn-as-you-build practical guide to building eight full-fledged applications using Xamarin.Forms, Xamarin Android, and Xamarin iOS. Each chapter includes a project, takes you through the process of building applications (such as a gallery Application, a text-to-speech service app, a GPS locator app, and a stock market app), and will show you how to deploy the application’s source code to a Google Cloud Source Repository. Other practical projects include a chat and a media-editing app, as well as other examples fit to adorn any developer’s utility belt. In the course of building applications, this book will teach you how to design and prototype professional-grade applications implementing performance and security considerations.
Table of Contents (14 chapters)
Xamarin Blueprints
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

The ALAssetLibrary


Jumping back into our iOS, we are going to use the ALAssetsLibrary class and call the Enumerate function by passing in the group type ALAssetsGroupType.SavedPhoto, the enumeration result delegate GroupEnumerator, and the error action that will be performed if an exception occurs.

Start by adding in a new .cs file for our iOS image handler:

Note

We are not going to use a static class with this object.

namespace Gallery.iOS 
{ 
    using System; 
    using System.Threading; 
 
    using UIKit; 
    using AssetsLibrary; 
    using Foundation; 
 
    /// <summary> 
    /// Image handler. 
    /// </summary> 
    public class ImageHandler 
    { 
        /// <summary> 
        /// The asset library. 
        /// </summary> 
        ALAssetsLibrary _assetLibrary; 
 
        /// <summary> 
        /// Initializes a new instance of the <see cref="Gallery.iOS.ImageHandler"/> class. 
        /// </summary> 
        public ImageHandler () 
        { 
            _assetLibrary = new ALAssetsLibrary(); 
            _assetLibrary.Enumerate(ALAssetsGroupType.SavedPhotos, GroupEnumerator, Console.WriteLine); 
        }  
    } 
} 

In our constructor, we create the new instance of the ALAssetsLibrary and call the Enumerate function; now let's add the GroupEnumerator delegate:

private void GroupEnumerator(ALAssetsGroup assetGroup, ref bool shouldStop) 
        { 
            if (assetGroup == null) 
            { 
                shouldStop = true; 
                NotifyAssetsLoaded (); 
 
                return; 
            } 
 
            if (!shouldStop) 
            { 
                assetGroup.Enumerate(AssetEnumerator); 
                shouldStop = false; 
            } 
        } 
 
        private void AssetEnumerator(ALAsset asset, nint index, ref bool shouldStop) 
        { 
            if (asset == null) 
            { 
                shouldStop = true; 
                return; 
            } 
 
            if (!shouldStop) 
            { 
                // add asset name to list 
                _assets.Add (asset.ToString()); 
                shouldStop = false; 
            } 
        } 
 
   private void NotifyAssetsLoaded() 
        { 
            if (AssetsLoaded != null) 
            { 
                AssetsLoaded (this, EventArgs.Empty); 
            } 
        } 

Notice the call to notify our event handler. This signals we have reached the end of the asset library, and we have retrieved all ALAsset in our gallery. We can now pull out a list of the file names, so we need to add another function that will pull out the ALAsset object synchronously:

public ALAsset SynchronousGetAsset(string filename) 
        { 
            ManualResetEvent waiter = new ManualResetEvent(false); 
            NSError error = null; 
            ALAsset result = null; 
            Exception exception;  
 
            ThreadPool.QueueUserWorkItem ((object state) => assetLibrary.AssetForUrl (new NSUrl (filename), (ALAsset asset) =>  
                { 
                    result = asset; 
                    waiter.Set (); 
                },  
                e =>  
                { 
                    error = e; 
                    waiter.Set (); 
                })); 
 
 
            if(!waiter.WaitOne (TimeSpan.FromSeconds (10))) 
                throw  new Exception("Error Getting Asset : Timeout, Asset=" + filename); 
 
            if (error != null) 
                throw new Exception (error.Description); 
 
            return result; 
        } 

Finally, we need a public function that will pull all the byte arrays and NSURL into an Enumerable of gallery items that we will use to populate the UITableView.

Tip

As this is only a demo, we are only going to take the first 100 items. If you would like another challenge, remove Take(100), and see if you can adjust the code to load thousands of images more efficiently.

foreach (var file in _assets.Take(100))  
            { 
                using (var asset = SynchronousGetAsset (file)) 
                { 
                    if (asset != null)  
                    { 
                        var thumbnail = asset.Thumbnail; 
                        var image = UIImage.FromImage (thumbnail); 
                        var jpegData = image.AsJPEG ().ToArray (); 
 
                        yield return new GalleryItem ()  
                        { 
                            Title = file, 
                            Date = asset.Date.ToString(), 
                            ImageData = jpegData, 
                            ImageUri = asset.AssetUrl.ToString () 
                        }; 
                    } 
                } 
            } 
        } 

Let's look a bit more closely at this function. We use the asset library object to pull out all the filenames we have in our gallery, then for each filename we pull out the ALAsset object, and from this we create a GalleryItem object for each, which takes the image data as a byte array from the ALAsset and the NSURL of the asset. Now let's create an instance of the ImageHandler inside our TableSource:

        private ImageHandler _imageHandler; 
 
        public TableSource (string[] items) 
        { 
            _galleryItems = new List<GalleryItem> (); 
            _imageHandler = new ImageHandler (); 
 
            foreach (var galleryItem in imageHandler.GetFiles ())  
            { 
                _galleryItems.Add (galleryItem); 
            } 
        } 

Excellent! Now we have our gallery items ready to display inside the table.

For the final piece of the iOS project, let's go back to our AppDelegate.cs file. We still need to implement the FinishedLaunching method. Our root controller is going to be a UINavigationController, which will use the MainController as the starting UIViewController:

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) 
        { 
            _window = new UIWindow (UIScreen.MainScreen.Bounds); 
 
            MainController mainController = new MainController(); 
 
            var rootNavigationController = new UINavigationController(); 
            rootNavigationController.PushViewController(mainController, false); 
 
            _window.RootViewController = rootNavigationController; 
            _window.MakeKeyAndVisible (); 
 
            return true; 
        } 

We also adjust the window bounds the main screen bounds and call the function on the window at the very end of MakeKeyAndVisible.