-
Notifications
You must be signed in to change notification settings - Fork 203
/
ImageEditorCustomization.cs
350 lines (291 loc) · 10.9 KB
/
ImageEditorCustomization.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#region Copyright Syncfusion Inc. 2001-2024.
// Copyright Syncfusion Inc. 2001-2024. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// [email protected]. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using Syncfusion.SfImageEditor.iOS;
using Foundation;
using UIKit;
using CoreGraphics;
using System.Drawing;
using Photos;
using System.IO;
namespace SampleBrowser
{
public class ImageEditorCustomization : SampleView
{
bool isReset, isUndo, isRedo, isRect, isText, isPath;
UIView RightView;
UIView overView;
UIView topView;
SfImageEditor sfImageEditor;
Object settings;
UITextField textView;
UIViewController presentController;
public ImageEditorCustomization()
{
}
public override void LayoutSubviews()
{
isReset=isUndo=isRedo=isRect=isText=isPath = false;
base.LayoutSubviews();
presentController = GetVisibleViewController();
UIView mainView = new UIView();
mainView.Frame = new CGRect(0, 0, Frame.Width, Frame.Height);
mainView.BackgroundColor = UIColor.Clear;
sfImageEditor = new SfImageEditor(new CGRect(mainView.Frame.X, mainView.Frame.Y, mainView.Frame.Width, mainView.Frame.Height));
sfImageEditor.Image = UIImage.FromBundle("Images/ImageEditor/Customize.jpg");
sfImageEditor.BackgroundColor = UIColor.Black;
sfImageEditor.ToolBarSettings.IsVisible = false;
//settings = new Object();
sfImageEditor.ItemSelected += (object sender, ItemSelectedEventArgs e) =>
{
RightView.Alpha = 1;
settings = e.Settings;
};
mainView.AddSubview(sfImageEditor);
/*--------------------------------------------------*/
//TopView
topView = new UIView();
topView.Frame = new CGRect(0, 15, Frame.Width, 25);
topView.Alpha = 0;
UIButton reset = new UIButton(new CGRect(0, 0, Frame.Width / 6, 25));
reset.BackgroundColor = UIColor.Clear;
reset.SetImage(UIImage.FromBundle("Images/ImageEditor/reset_customization.png"), UIControlState.Normal);
reset.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
reset.TouchUpInside += Reset_TouchUpInside;
topView.AddSubview(reset);
UIButton redo = new UIButton(new CGRect(Frame.Width / 6, 0, Frame.Width / 6, 25));
redo.BackgroundColor = UIColor.Clear;
redo.SetImage(UIImage.FromBundle("Images/ImageEditor/redo_customization.png"), UIControlState.Normal);
redo.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
redo.TouchUpInside += Redo_TouchUpInside;
redo.Alpha = 0;
topView.AddSubview(redo);
UIButton undo = new UIButton(new CGRect(2 * (Frame.Width / 6), 0, Frame.Width / 6, 25));
undo.BackgroundColor = UIColor.Clear;
undo.SetImage(UIImage.FromBundle("Images/ImageEditor/undo_customization.png"), UIControlState.Normal);
undo.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
undo.TouchUpInside += Undo_TouchUpInside;
topView.AddSubview(undo);
UIButton rect = new UIButton(new CGRect(3 * (Frame.Width / 6), 0, Frame.Width / 6, 25));
rect.BackgroundColor = UIColor.Clear;
rect.SetImage(UIImage.FromBundle("Images/ImageEditor/rect_customization.png"), UIControlState.Normal);
rect.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
rect.TouchUpInside += Rect_TouchUpInside;
topView.AddSubview(rect);
UIButton text = new UIButton(new CGRect(4 * (Frame.Width / 6), 0, Frame.Width / 6, 25));
text.BackgroundColor = UIColor.Clear;
text.SetImage(UIImage.FromBundle("Images/ImageEditor/text_customization.png"), UIControlState.Normal);
text.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
text.TouchUpInside += Text_TouchUpInside;
topView.AddSubview(text);
UIButton path = new UIButton(new CGRect(5 * (Frame.Width / 6), 0, Frame.Width / 6, 25));
path.SetImage(UIImage.FromBundle("Images/ImageEditor/pen_customization.png"), UIControlState.Normal);
path.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
path.TouchUpInside += Path_TouchUpInside;
topView.AddSubview(path);
/*----------------------------------------------------------*/
//BottomView
UIView bottomView = new UIView();
bottomView.Frame = new CGRect(10, Frame.Height - 50, Frame.Width, 30);
bottomView.BackgroundColor = UIColor.Clear;
textView = new UITextField(new CGRect(20, 0, Frame.Width - 100, 30));
textView.Layer.CornerRadius = 10.0f;
textView.Layer.BorderColor = UIColor.White.CGColor;
textView.Layer.BorderWidth = 2;
textView.TextColor = UIColor.White;
textView.Placeholder = "Enter a Caption";
textView.Enabled = true;
textView.ResignFirstResponder();
textView.MultipleTouchEnabled = true;
bottomView.AddSubview(textView);
UIButton share = new UIButton(new CGRect(Frame.Width - 70, 0, 50, 30));
share.BackgroundColor = UIColor.Clear;
share.SetImage(UIImage.FromBundle("Images/ImageEditor/share_customization.png"), UIControlState.Normal);
share.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
share.TouchUpInside += Share_TouchUpInside;
bottomView.AddSubview(share);
/*--------------------------------------------------*/
//RightView
RightView = new UIView();
RightView.Frame = new CGRect(Frame.Width - 50, 20, 30, Frame.Height );
RightView.BackgroundColor = UIColor.Clear;
//Color Collection
UIColor[] array = new UIColor[10]{ UIColor.FromRGB(68,114,196)
,UIColor.FromRGB(237,125,49)
,UIColor.FromRGB(255,192,0)
,UIColor.FromRGB(112,173,71)
,UIColor.FromRGB(91,155,213)
,UIColor.FromRGB(193,193,193)
,UIColor.FromRGB(111,111,226)
,UIColor.FromRGB(226,105,174)
,UIColor.FromRGB(158,72,14)
,UIColor.FromRGB(153,115,0)
};
int y = (int)(this.Frame.Height / 2)-175;
for (int i = 0; i < 10; i++)
{
UIButton colorButton = new UIButton();
colorButton.Frame = new CGRect(3, y + 5, 25, 25);
colorButton.Layer.CornerRadius = 10;
y = y + 30;
colorButton.BackgroundColor = array[i];
colorButton.TouchUpInside += ColorButton_TouchUpInside;
RightView.Add(colorButton);
}
mainView.AddSubview(RightView);
mainView.AddSubview(topView);
mainView.AddSubview(bottomView);
RightView.Alpha = 0;
overView = new UIView();
overView.Frame = new CGRect(0, 0, Frame.Width, Frame.Height);
overView.BackgroundColor = UIColor.Clear;
overView.Alpha = 1f;
UITapGestureRecognizer tapped = new UITapGestureRecognizer(TapCarrierLabel);
overView.AddGestureRecognizer(tapped);
AddSubview(mainView);
AddSubview(overView);
}
public void TapCarrierLabel(UITapGestureRecognizer uitgr)
{
overView.Alpha = 0;
topView.Alpha = 1;
}
void Path_TouchUpInside(object sender, EventArgs e)
{
sfImageEditor.AddShape(ShapeType.Path, new PenSettings());
settings = null;
isPath = true;
isReset = false;
isUndo = false;
isRedo = false;
isRect = false;
isText = false;
RightView.Alpha = 1;
}
void Text_TouchUpInside(object sender, EventArgs e)
{
settings = null;
isPath = false;
isReset = false;
isUndo = false;
isRedo = false;
isRect = false;
isText = true;
RightView.Alpha = 1;
sfImageEditor.AddText("Text", new TextSettings());
}
void Rect_TouchUpInside(object sender, EventArgs e)
{
settings = null;
isPath = false;
isReset = false;
isUndo = false;
isRedo = false;
isRect = true;
isText = false;
RightView.Alpha = 1;
sfImageEditor.AddShape(ShapeType.Rectangle, new PenSettings());
}
void Redo_TouchUpInside(object sender, EventArgs e)
{
isPath = false;
isReset = false;
isUndo = false;
isRedo = true;
isRect = false;
isText = false;
RightView.Alpha = 0;
sfImageEditor.Redo();
}
void Undo_TouchUpInside(object sender, EventArgs e)
{
isPath = false;
isReset = false;
isUndo = true;
isRedo = false;
isRect = false;
isText = false;
RightView.Alpha = 0;
sfImageEditor.Undo();
}
void Reset_TouchUpInside(object sender, EventArgs e)
{
isPath = false;
isReset = true;
isUndo = false;
isRedo = false;
isRect = false;
isText = false;
RightView.Alpha = 0;
sfImageEditor.Reset();
overView.Alpha = 1;
topView.Alpha = 0;
}
void Share_TouchUpInside(object sender, EventArgs e)
{
sfImageEditor.Save();
sfImageEditor.ImageSaved += SfImageEditor_ImageSaved;
}
void ColorButton_TouchUpInside(object sender, EventArgs e)
{
var colorSelected = (sender as UIButton).BackgroundColor;
if (isPath)
{
sfImageEditor.AddShape(ShapeType.Path, new PenSettings() { Color = colorSelected });
}
if (settings is TextSettings)
{
(settings as TextSettings).Color = colorSelected;
}
if (settings is PenSettings)
{
(settings as PenSettings).Color = colorSelected;
//sfImageEditor.AddShape(ShapeType.Rectangle, new PenSettings() { Color = colorSelected, StrokeWidth = 5 });
}
RightView.Alpha = 1;
}
void SfImageEditor_ImageSaved(object sender, ImageSavedEventArgs e)
{
string[] str = e.Location.Split('/');
PHFetchResult assetResult = PHAsset.FetchAssetsUsingLocalIdentifiers(str, null);
PHAsset asset = assetResult.firstObject as PHAsset;
PHImageManager.DefaultManager.RequestImageData(asset, null, async (data, dataUti, orientation, info) =>
{
UIImage newImage = new UIImage(data);
var items = new NSObject[] { newImage };
var activityController = new UIActivityViewController(items, null);
NSString[] excludedActivityTypes = null;
if (excludedActivityTypes != null && excludedActivityTypes.Length > 0)
activityController.ExcludedActivityTypes = excludedActivityTypes;
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
if (activityController.PopoverPresentationController != null)
{
activityController.PopoverPresentationController.SourceView = presentController.View;
}
}
await presentController.PresentViewControllerAsync(activityController, true);
});
}
internal static UIViewController GetVisibleViewController()
{
var rootController = UIApplication.SharedApplication.KeyWindow.RootViewController;
if (rootController.PresentedViewController == null)
return rootController;
if (rootController.PresentedViewController is UINavigationController)
{
return ((UINavigationController)rootController.PresentedViewController).TopViewController;
}
if (rootController.PresentedViewController is UITabBarController)
{
return ((UITabBarController)rootController.PresentedViewController).SelectedViewController;
}
return rootController.PresentedViewController;
}
}
}