Skip to content

Instantly share code, notes, and snippets.

@nicwise nicwise/scale.cs
Last active Nov 13, 2018

Embed
What would you like to do?
Scale and Rotate an image in iOS / MonoTouch, using the EXIF data
//MIT license
public static UIImage ScaleImage(UIImage image, int maxSize)
{
UIImage res;
using (CGImage imageRef = image.CGImage)
{
CGImageAlphaInfo alphaInfo = imageRef.AlphaInfo;
CGColorSpace colorSpaceInfo = CGColorSpace.CreateDeviceRGB();
if (alphaInfo == CGImageAlphaInfo.None)
{
alphaInfo = CGImageAlphaInfo.NoneSkipLast;
}
int width, height;
width = imageRef.Width;
height = imageRef.Height;
if (height >= width)
{
width = (int)Math.Floor((double)width * ((double)maxSize / (double)height));
height = maxSize;
}
else
{
height = (int)Math.Floor((double)height * ((double)maxSize / (double)width));
width = maxSize;
}
CGBitmapContext bitmap;
if (image.Orientation == UIImageOrientation.Up || image.Orientation == UIImageOrientation.Down)
{
bitmap = new CGBitmapContext(IntPtr.Zero, width, height, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
}
else
{
bitmap = new CGBitmapContext(IntPtr.Zero, height, width, imageRef.BitsPerComponent, imageRef.BytesPerRow, colorSpaceInfo, alphaInfo);
}
switch (image.Orientation)
{
case UIImageOrientation.Left:
bitmap.RotateCTM((float)Math.PI / 2);
bitmap.TranslateCTM(0, -height);
break;
case UIImageOrientation.Right:
bitmap.RotateCTM(-((float)Math.PI / 2));
bitmap.TranslateCTM(-width, 0);
break;
case UIImageOrientation.Up:
break;
case UIImageOrientation.Down:
bitmap.TranslateCTM(width, height);
bitmap.RotateCTM(-(float)Math.PI);
break;
}
bitmap.DrawImage(new Rectangle(0, 0, width, height), imageRef);
res = UIImage.FromImage(bitmap.ToImage());
bitmap = null;
}
return res;
}
@mjracca

This comment has been minimized.

Copy link

commented Jun 3, 2013

Thanks!! Rotation works perfectly!

@tonyfonager

This comment has been minimized.

Copy link

commented Nov 8, 2013

You are the champ - thanks ;-)

@schmidan

This comment has been minimized.

Copy link

commented Dec 6, 2013

I was building the same thing but got problems with Orientation. Your Snipped works perfectly. Thanks a bunch!

@adilo

This comment has been minimized.

Copy link

commented Dec 25, 2013

It worked like a charm ! Thank you !

@felixcollins

This comment has been minimized.

Copy link

commented Feb 24, 2014

Note that the BytesPerRow is not being recalculated for the CGBitmapContext ctor despite scaling the size. This can cause an exception if the row width is not great enough ("CGBitmapContextCreate: invalid data bytes/row:"). Set it to 0 instead of imageRef.BytesPerRow to let CoreGraphics calculate it. Thanks Mr Wise!

@jacksierkstra

This comment has been minimized.

Copy link

commented Jul 18, 2014

This works very good! I wonder if there is a version for Monodroid aswell?

@yehe01

This comment has been minimized.

Copy link

commented Aug 26, 2016

Thanks.

@armaganX

This comment has been minimized.

Copy link

commented May 24, 2018

What is the maxSize parameter for no changing photo's resolution ?

@drew-neely

This comment has been minimized.

Copy link

commented Jun 18, 2018

Thanks! This helped me a ton.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.