UIAlertView + UIImage

Today I wanted some debug code to easily display an UIImage to check its orientation. At first I was going to just add it to my main view then remove it after a few seconds, but then realized a modified UIAlertView would be better since it could be displayed fire-and-forget style like [alert show]; [alert release];, it wouldn’t go away before I had a chance to look, and there could be a title to remind me what I was looking at.

Some trial-and-error was involved to get the message newlines & image position right, but here’s what I came up with. It displays the image scaled to 100 units height.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"\n\n\n\n\n" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGFloat imageHeight = 100;
CGFloat imageWidth = imageHeight * image.size.width / image.size.height;
imageView.frame = CGRectMake(floor((284 - imageWidth)/2), 47, imageWidth, imageHeight);
[alert addSubview:imageView];
[alert show];
[alert release];

One Comment on “UIAlertView + UIImage”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s