UIImage Cropping Library for Xamarin iOS

We just released a binding project for Tim Oliver’s TOCropViewController library on our public GitHub repository . This iOS binding project provides a view controller allowing users to crop UIImage objects.

In one of our Xamarin iOS projects, we needed a way to crop images. We looked for existing libraries and could not find a Xamarin iOS one that satisfied our needs.

One library drew our attention though: TOCropViewController, a UIIMage cropping project made by Tim Oliver, allowing users to crop and rotate UIImage with an iOS look and feel.

The only problem being: it is an Objective-C library. We decided to create a binding project, using Objective Sharpie.

The process to create a binding library project and exposing APIs similar to .NET is already described here and here. This project has something new to the process that neither of our previous blog posts talked about: How to support the iOS simulators.

Simulator support for iOS Bindings

To support the iOS simulator, you need to compile the Objective-C code with the i386 architecture as well. However iPhone devices don’t support it so we need to modify the build configuration in the Xcode project. First we need to modify the valid architecture of the project’s target (here TOCropViewController) to have both Any iOS SDK and Any iOS Simulator SDK. The iOS SDK should have arm64, armv7 and armv7s while the iOS Simulator SDK should have i386. A similar modification should be done to Architecture. We need to add both iOS Simulator SDK and iOS SDK with the correct supported architectures (the simulator having i386 and iOS having at least armv7 and arm64).

Screen-Shot-2016-05-18-at-4.20.59-PM

Then we need to build the archive of both the simulator and the iOS SDK by using the Archive option under the Product menu. Finally we needed to get the 2 resultant .a files and merge them using lipo.

After we run the archive tool, we just have to drop the final fat .a in our binding project as well as the ApiDefinition.cs and StructsAndEnums.cs files produced by ObjectiveSharpie as described here. Finally, we just have to add the ShowCropViewAsync function to expose the .NET like call (hiding the need to use the delegates inside the binding library and therefore having to do less boilerplate code on the app side).

How to use this binding project

  • Add the project to your Xamarin solution

  • Add a reference to this project to your iOS project

Code sample

A class to encapsulate the cropping service

Usage in a ViewModel

Screenshots

Simulator Screen Shot May 26, 2016, 11.49.34 AM Simulator Screen Shot May 26, 2016, 11.49.59 AM Simulator Screen Shot May 26, 2016, 11.50.01 AM

Recent Posts
Showing 4 comments
  • raja
    Reply

    how to use this solution in xamarin pcl application for ios, can you explain me detail…

    • Dominique Savoie
      Reply

      To add this solution to a Xamarin PCL application you will need to do the following:

      Get the source code from our Git repository (the binding project) and compile it.
      Add the created DLL to your Xamarin.iOS project inside the solution of your application.
      Make an interface in in a project that is referenced by both the iOS project and the PCL project you want to use the crop in. Add a method with the signature of the Crop method example (see above)
      Make a service class in your Xamarin.iOS project with the sample code we provide above (the Crop method) and add the interface to the class (the crop method should be implementing the interface.
      Use an IoC container to Register an instance of the service class with the interface (If you don’t have one, Funq will probably do, or MVVMCross does come with it’s own IoC container).

      Now in the PCL class you want to use this crop binding, you simply resolve the dependency from the IoC container (or get the service injected in the constructor with the Interface as the type) and use the Crop method.

      PS: You might need to adjust the Crop method to for being called on the UI thread, if where you are calling is not on the UI.

  • BB
    Reply

    HI this looks like a nice project!
    But i just cant get it to run, it builds fine, but with the sample code from github it crashes with a nullreference.

    Can you please make an updated project with this binding included in the project ( a sample )

    Thakn you!

  • BB
    Reply

    Can you please provide a fully working sample xamarin ios project. because I cant get this to work. The sample come in the readme on github doesnt make any sense ( why not await the Task ?) It feels like something you wrote but didnt test tbh..

    I would be very greatfull!

    Thank you

Leave a Comment