jmawebtech

centerImageMT

Oct 16th, 2012
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using MonoTouch.Dialog;
  3. using MonoTouch.Foundation;
  4. using MonoTouch.UIKit;
  5. using System.Drawing;
  6.  
  7. namespace MenuFinderMT
  8. {
  9.     //cell.ImageView.ContentMode = UIViewContentMode.Center;
  10.     public class JMAImageElement : Element, IElementSizing
  11.     {
  12.         static NSString skey = new NSString ("About_Image");
  13.         UIImage image;
  14.         float limit = 600;
  15.         DialogViewController dvc;
  16.  
  17.         public JMAImageElement (string caption, byte[] bytes, DialogViewController dvc)
  18.             : base(string.Empty)
  19.         {
  20.             this.dvc = dvc;
  21.             SizeF sz = new SizeF (600, 600);
  22.  
  23.             //320 x 480 old iPhone support
  24.             if(dvc.View.Frame.Width < 600)
  25.             {
  26.                 limit = (float)dvc.View.Frame.Width - 200;
  27.                 sz = new SizeF(300, 300);
  28.             }
  29.  
  30.  
  31.             this.image = Scale (GetImagefromByteArray(bytes), sz);
  32.         }
  33.        
  34.         public override UITableViewCell GetCell (UITableView tv)
  35.         {
  36.             var cell = tv.DequeueReusableCell (skey);
  37.             if (cell == null) {
  38.                 cell = new UITableViewCell (UITableViewCellStyle.Default, skey)
  39.                 {
  40.                     SelectionStyle = UITableViewCellSelectionStyle.None,
  41.                     Accessory = UITableViewCellAccessory.None,
  42.                 };
  43.             }
  44.             if (null != image) {
  45.                 cell.ImageView.Image = image;
  46.                 cell.ImageView.ContentMode = UIViewContentMode.Center;
  47.             }
  48.  
  49.  
  50.             return cell;
  51.         }
  52.        
  53.         public float GetHeight (UITableView tableView, NSIndexPath indexPath)
  54.         {
  55.             float height = 100;
  56.             if (null != image)
  57.                 height = image.Size.Height;
  58.             return height;
  59.         }
  60.  
  61.         UIImage GetImagefromByteArray (byte[] imageBuffer)
  62.         {
  63.             NSData imageData = NSData.FromArray (imageBuffer);
  64.             return UIImage.LoadFromData (imageData);
  65.         }
  66.  
  67.         UIImage Scale (UIImage source, SizeF newSize)
  68.         {
  69.             if ((dvc.View.Frame.Size.Width > limit) || (dvc.View.Frame.Size.Height > limit)) {
  70.                 UIGraphics.BeginImageContext (newSize);
  71.                 var context = UIGraphics.GetCurrentContext ();
  72.                 context.TranslateCTM (0, newSize.Height);
  73.                 context.ScaleCTM (1f, -1f);
  74.            
  75.                 context.DrawImage (new RectangleF (0, 0, newSize.Width, newSize.Height), source.CGImage);
  76.            
  77.                 var scaledImage = UIGraphics.GetImageFromCurrentImageContext ();
  78.                 UIGraphics.EndImageContext ();
  79.            
  80.                 return scaledImage;
  81.             } else {
  82.                 return source;
  83.             }
  84.         }
  85.        
  86.         public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
  87.         {
  88.             //base.Selected(dvc, tableView, path);
  89.             tableView.DeselectRow (indexPath, true);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment