不完整截图功能
using System;
using CoreGraphics;
using UIKit;
using Foundation;
using Pasasoft.Fep.Drawing;
using Pasasoft.Utilities;
using Pasasoft.Fep.Resources;
using Pasasoft.Localization;
namespace Pasasoft.Fep.UIKit
{
public class ImageViewController:UIViewController
{
private UIImage image;
private UIImageView imageView;
private UIView maskView;
public Action<UIImage> DoneAction{ get; set;}
private UIBarButtonItem cropItem;
private UIBarButtonItem rotationCW;
private UIBarButtonItem rotationCCW;
public ImageViewController()
{
var cancelButton = new UIBarButtonItem (UIBarButtonSystemItem.Cancel,null);
cancelButton.Clicked += delegate {Dismiss();};
NavigationItem.LeftBarButtonItem = cancelButton;
var doneButton = new UIBarButtonItem (UIBarButtonSystemItem.Done, null);
doneButton.Clicked += delegate {
var image = GetTargetImage();
if(null != image){
if(null != DoneAction)
DoneAction.Invoke(image);
Dismiss();
}
else{
AlertHelper.ShowAlert(Strings.CropRegion.Localize());
}
};
NavigationItem.RightBarButtonItem = doneButton;
cropItem = new UIBarButtonItem ();
cropItem.Image= UIImage.FromBundle (ImageFiles.Crop25);
cropItem.Clicked += delegate {
Crop();
};
rotationCW = new UIBarButtonItem ();
rotationCW.Image = UIImage.FromBundle (ImageFiles.RotationCW);
rotationCW.Clicked += delegate {
Clear();
Rotation(true);
};
rotationCCW = new UIBarButtonItem ();
rotationCCW.Image = UIImage.FromBundle (ImageFiles.RotationCCW);
rotationCCW.Clicked += delegate {
Clear();
Rotation(false);
};
ToolbarItems = new UIBarButtonItem[]{ cropItem, rotationCCW, rotationCW};
EdgesForExtendedLayout = UIRectEdge.None;
}
private void Pan(UIPanGestureRecognizer gestureRecognizer)
{
if (null != maskView && maskView.Superview != null) {
var p1 = gestureRecognizer.LocationInView (View);
var p2 = gestureRecognizer.TranslationInView (View);
var p3 = new CGPoint (p1.X - p2.X, p1.Y - p2.Y);
var frame = DrawHelper.GetBound (p1, p3);
maskView.Frame = frame;
}
}
private UIImage GetTargetImage()
{
if (null == maskView || maskView.Superview == null)
return image;
var imageViewFrame = imageView.Frame;
CGRect imageFrame = new CGRect ();
var scaleImage = image.Size.Width / image.Size.Height;
var scaleImageView = imageView.Frame.Width / imageView.Frame.Height;
if (image.Size.Width < imageViewFrame.Width && image.Size.Height < imageViewFrame.Height) {
imageFrame = imageViewFrame;
}else if (scaleImageView > scaleImage) {
var imageWidth = image.Size.Width / image.Size.Height * imageViewFrame.Height;
var imageX = (imageViewFrame.Width - imageWidth) / 2;
imageFrame = new CGRect (imageX, imageViewFrame.Y, imageWidth, imageViewFrame.Height);
}else if (scaleImageView <= scaleImage) {
var imageHeight = image.Size.Height / image.Size.Width * imageViewFrame.Width;
var imageY = (imageViewFrame.Height - imageHeight) / 2;
imageFrame = new CGRect (imageViewFrame.X, imageY, imageViewFrame.Width, imageHeight);
}
var frame = CGRect.Intersect(maskView.Frame,imageFrame);
if (frame.IsEmpty)
return null;
var cropFrame = new CGRect ();
var scaleX = image.Size.Width / imageFrame.Width;
var scaleY = image.Size.Height / imageFrame.Height;
cropFrame.X = (frame.X - imageFrame.X) * scaleX;
cropFrame.Y = (frame.Y - imageFrame.Y) * scaleY;
cropFrame.Width = frame.Width * scaleX;
cropFrame.Height = frame.Height * scaleY;
return image.Crop(cropFrame);
}
private void Clear()
{
if(null != maskView)
maskView.RemoveFromSuperview ();
cropItem.Image = UIImage.FromBundle (ImageFiles.Crop25);
cropItem.Style = UIBarButtonItemStyle.Plain;
}
private void DoCrop()
{
if (null == maskView) {
maskView = new UIView ();
maskView.BackgroundColor = UIColor.FromRGB(0,0,96);
maskView.Alpha = .5f;
}
maskView.Frame = CGRect.Empty;
if(maskView.Superview == null)
View.AddSubview (maskView);
cropItem.Image = UIImage.FromBundle (ImageFiles.CropSelect25);
cropItem.Style = UIBarButtonItemStyle.Done;
}
private void Crop()
{
if (cropItem.Style == UIBarButtonItemStyle.Plain)
DoCrop ();
else
Clear ();
}
private void Rotation(bool isClockwise)
{
nfloat rotate, translateX, translateY;
CGRect rect = new CGRect (0, 0, image.Size.Height, image.Size.Width);
if (isClockwise) {
rotate = 3 * (nfloat)Math.PI / 2;
translateX = -rect.Size.Height;
translateY = 0;
} else {
rotate = (nfloat)Math.PI / 2;
translateX = 0;
translateY = -rect.Size.Width;
}
UIGraphics.BeginImageContext (rect.Size);
CGContext context = UIGraphics.GetCurrentContext ();
context.TranslateCTM ( 0, rect.Height);
context.ScaleCTM (1, -1);
context.RotateCTM (rotate);
context.TranslateCTM (translateX, translateY);
context.DrawImage (new CGRect (0, 0, rect.Size.Height, rect.Size.Width), image.CGImage);
image = UIGraphics.GetImageFromCurrentImageContext ();
imageView.Image = image;
}
private void Dismiss()
{
NavigationController.PresentingViewController.DismissViewController(true,null);
}
public UIImage Image
{
get{ return image; }
set
{
image = value;
if (IsViewLoaded)
imageView.Image = image;
}
}
public override void LoadView ()
{
var view = new UIView (UIScreen.MainScreen.Bounds);
view.BackgroundColor = UIColor.White;
if (null != image)
imageView = new UIImageView (image);
else
imageView = new UIImageView (UIScreen.MainScreen.Bounds);
imageView.TranslatesAutoresizingMaskIntoConstraints = false;
imageView.ContentMode = UIViewContentMode.ScaleAspectFit;
view.AddSubview (imageView);
View = view;
var gestureRecognizer = new UIPanGestureRecognizer (Pan);
gestureRecognizer.MinimumNumberOfTouches = 1;
gestureRecognizer.MaximumNumberOfTouches = 1;
view.AddGestureRecognizer (gestureRecognizer);
}
public override void ViewDidLayoutSubviews ()
{
base.ViewDidLayoutSubviews ();
var rect = UIScreen.MainScreen.Bounds;
var naviHeight = this.NavigationController.NavigationBar.Frame.Bottom + this.NavigationController.Toolbar.Frame.Height;
imageView.Frame = new CGRect (0, 0, rect.Width, rect.Height - naviHeight);
}
}
}