Images captured by iOS show up rotated on Android

When you capture a photo with TakePhotoAsync on the iOS, regardless of the SaveMetaData flag or RotateImage, it might be stored rotated. However, you won’t notice it on the iOS itself, or on Windows, since the orientation information is stored in the EXIF metadata of the captured JPEG file.

iOS or Windows show the image correctly because they take into account the EXIF orientation, but the Image view of Xamarin.Forms on Android ignores this flag, thus making the image show up as-is, i.e. rotated.

If you store the image in a different format than JPEG (e.g. PNG) on the iOS, you can also see the image rotated there, because PNG doesn’t have an orientation flag.

If you are using Xamarin.Forms, consider using CachedImage from FFImageLoading instead of the built-in Image view. CachedImage looks at the orientation information on Android, and rotates the image accordingly before rendering it.

However, if you are uploading the image to an ASP.NET core application and need to process it, you can rotate it manually based on the EXIF information. .NET core by itself doesn’t have image manipulation capabilities, but you can install the CoreCompat.System.Drawing NuGet package, which is a port of System.Drawing for .NET core.

First, load the image as an Image object:

var image = Image.FromFile(path);

Then see if it has the rotation value, and rotate accordingly:

if (image.PropertyIdList.Contains(0x112))
    int rotationValue = image.GetPropertyItem(0x112).Value[0];
    if (rotationValue == 8) 
        image.RotateFlip(rotateFlipType: RotateFlipType.Rotate270FlipNone);
    else if (rotationValue == 3) 
        image.RotateFlip(rotateFlipType: RotateFlipType.Rotate180FlipNone);
    else if (rotateValue == 6)
        image.RotateFlip(rotateFlipType: RotateFlipType.Rotate90FlipNone);

Optionally, you can save the image on the original file:



Checking whether a DLL is 32-bit or 64-bit

A very easy method to check whether a particular DLL is x86-64 compatible or not is by using the file command in Linux. If you have bash installed on your Windows, you can type:

file MY_DLL.dll

If it’s a 64-bit DLL, you will see:

PE32+ executable (DLL) (GUI) x86-64, for MS Windows

Otherwise, it prints:

PE32 executable (DLL) (GUI) Intel 80386, for MS Windows

mpc.exe outputs an empty Resolver

In order to use MessagePack on the iOS or in general with AOT, you need to use mpc.exe to generate a resolver before building your main projects. Since Xamarin and ASP.NET are all about code sharing, usually a good practice is to create a shared project or a library containing model definitions, and referencing it in both server and client projects.

This raises the question as where to embed the script to run mpc.exe in order to facilitate the automatic generation and updating the resolver class.

One problem is that mpc.exe does not work with Shared Projects (.shproj), and as of now only accepts .csproj files as the input. So one thing to do is to create a stub/dummy project containing only the model classes, and feeding it to mpc.exe.

The frustration comes when mpc.exe doesn’t like an input. In such cases, it merely generates a blank useless resolver class (e.g. MessagePackGenerated.cs) without giving any errors. You have to manually investigate everything in order to find out what goes wrong.

One of the main cases in which I realized that mpc.exe doesn’t work properly, is when you don’t have the correct version of MessagePack NuGet installed on the stub project. Make sure you install MessagePack in that project.

Resetting Windows 10 bash user password

If you have forgotten your bash user password on Windows 10, there is a way to log in as root to reset that user’s password using passwd.

First, make sure all bash windows are closed. This is important as if one bash instance is open, this may not work:

  1. Open a Command Prompt as Administrator. You can do that by typing cmd in the Start menu and pressing CTRL+Shift+Enter.
  2. Run: ubuntu config --default-user root
  3. The previous command tells bash to log in as root the next time it’s started. So you can now run a rooted bash by typing bash.
  4. Now you can reset any user’s password by entering passwd myuser
  5. Finally, exit bash, and in the elevated Command Prompt, type ubuntu config --default-user myuser to change the default bash login to your bash user.

Solving “Native linking failed” issues running Xamarin.iOS on Simulator

Some external libraries will cause the project fail to build on the iPhoneSimulator (e.g. x86_64) target. The resulting errors can be:

Native linking failed. Please review the build log.
Native linking error: 1 duplicate symbol for architecture x86_64
Duplicate symbol in: /Users/admin/Library/Caches/Xamarin/mtbs/builds/.../obj/iPhoneSimulator/Debug/mtouch-cache/x86_64/main.o (Location related to previous error)
Duplicate symbol in: /Users/admin/Library/Caches/Xamarin/mtbs/builds/.../obj/iPhoneSimulator/Debug/mtouch-cache/leveldb-library(leveldb_main.o) (Location related to previous error)
Native linking failed, duplicate symbol: '_main'.

Sometimes you can make the build work by adding --registrar:static in the Additional mtouch arguments (MtouchExtraArgs) setting of the iOS project properties.