-
Notifications
You must be signed in to change notification settings - Fork 203
/
ImageEditorCustomView.cs
319 lines (267 loc) · 12.7 KB
/
ImageEditorCustomView.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
#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 System.Collections.ObjectModel;
using System.Collections.Generic;
using UIKit;
using CoreGraphics;
using Syncfusion.SfImageEditor.iOS;
using Foundation;
namespace SampleBrowser
{
public class ImageEditorCustomView : SampleView
{
UINavigationController navigationController;
UIView mainView;
UILabel label;
UIButton imageView1,imageView2,imageView3;
int padding = 3;
public ImageEditorCustomView()
{
mainView = new UIView();
nfloat TotalWidth = Frame.Width - 10;
float renderWidth = (float)TotalWidth / 3;
mainView.Frame = new CGRect(0, 0, Frame.Width, Frame.Height);
/*------Header Label-------*/
label = new UILabel(new CGRect(20, 50, TotalWidth, 25));
label.BackgroundColor = UIColor.Clear;
label.Font = UIFont.SystemFontOfSize(25);
label.TextAlignment = UITextAlignment.Left;
label.TextColor = UIColor.Gray;
label.Text = "Sample Pictures";
imageView1 = new UIButton(new CGRect(padding, 100, (TotalWidth / 3) - padding, 150));
imageView1.SetImage(UIImage.FromBundle("Images/ImageEditor/CustomViewImage1.png"), UIControlState.Normal);
imageView1.ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
imageView1.Layer.BorderColor = UIColor.LightGray.CGColor;
imageView1.Layer.BorderWidth = 0.5f;
float renderHeight = GetImageHeight(imageView1, renderWidth);
imageView1.Frame = new CGRect(0, 100, renderWidth, renderHeight);
imageView1.TouchDown += (sender, e) =>
{
navigationController.PushViewController(new CustomViewViewController(imageView1.CurrentImage, 0), false);
};
mainView.AddSubview(imageView1);
imageView2 = new UIButton(new CGRect((TotalWidth / 3) + (1 * padding), 100, (TotalWidth / 3) - padding, 150));
imageView2.SetImage(UIImage.FromBundle("Images/ImageEditor/CustomViewImage2.png"), UIControlState.Normal);
imageView2.ImageView.ContentMode = UIViewContentMode.ScaleToFill;
imageView2.Layer.BorderColor = UIColor.LightGray.CGColor;
imageView2.Layer.BorderWidth = 0.5f;
float renderHeight1 = GetImageHeight(imageView2, renderWidth);
imageView2.Frame = new CGRect(TotalWidth / 3, 100, renderWidth, renderHeight1);
mainView.AddSubview(imageView2);
imageView2.TouchDown += (sender, e) =>
{
navigationController.PushViewController(new CustomViewViewController(imageView2.CurrentImage, 1), false);
};
imageView3 = new UIButton(new CGRect((2 * (TotalWidth / 3)) + (1 * padding), 100, (TotalWidth / 3) - padding, 150));
imageView3.SetImage(UIImage.FromBundle("Images/ImageEditor/CustomViewImage3.png"), UIControlState.Normal);
imageView3.ImageView.ContentMode = UIViewContentMode.ScaleToFill;
imageView3.Layer.BorderColor = UIColor.LightGray.CGColor;
imageView3.Layer.BorderWidth = 0.5f;
float renderHeight2 = GetImageHeight(imageView3, renderWidth);
imageView3.Frame = new CGRect((2 * TotalWidth / 3), 100, renderWidth, renderHeight2);
imageView3.TouchDown += (sender, e) =>
{
navigationController.PushViewController(new CustomViewViewController(imageView3.CurrentImage, 2), false);
};
mainView.AddSubview(imageView3);
AddSubview(label);
AddSubview(mainView);
}
public override void MovedToSuperview()
{
base.MovedToSuperview();
var view = Superview;
var window = UIApplication.SharedApplication.KeyWindow;
navigationController = window.RootViewController as UINavigationController;
}
public override void LayoutSubviews()
{
base.LayoutSubviews();
nfloat TotalWidth = Frame.Width - 20;
mainView.Frame = new CGRect(0, 0, TotalWidth, Frame.Height);
float renderWidth = (float)(TotalWidth / 3)-padding;
label.Frame = new CGRect(20, 50, TotalWidth, 25);
float renderHeight1 = GetImageHeight(imageView1, renderWidth);
imageView1.Frame = new CGRect(padding, 100, renderWidth, renderHeight1);
float renderHeight2 = GetImageHeight(imageView2, renderWidth);
imageView2.Frame = new CGRect((TotalWidth / 3)+padding, 100, renderWidth, renderHeight2);
float renderHeight3 = GetImageHeight(imageView3, renderWidth);
imageView3.Frame = new CGRect((2 * TotalWidth / 3)+padding, 100, renderWidth, renderHeight3);
}
private float GetImageHeight(UIButton button, float renderWidth)
{
float originalWidth = (float)button.ImageView.Image.Size.Width;
float originalHeight = (float)button.ImageView.Image.Size.Height;
float renderedHeight = (renderWidth * originalHeight) / originalWidth;
return renderedHeight;
}
}
public class CustomViewViewController : UIViewController
{
UIImage image;
public CustomViewViewController(UIImage image, int index)
{
this.image = image;
selectedIndex = index;
}
SfImageEditor imageEditor;
UIViewController presentController;
int selectedIndex = 0;
List<ToolbarItem> CustomViewItems;
CustomViewSettings customViewSettings;
bool isReplaced; string imageName;
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
presentController = GetVisibleViewController();
imageEditor = new SfImageEditor(new CGRect(View.Frame.Location.X, 60, View.Frame.Size.Width, View.Frame.Size.Height - 60));
imageEditor.SetToolbarItemVisibility("text,transform,shape,path,effects", false);
CustomViewItems = new List<ToolbarItem>()
{
new CustomToolbarItem(){ CustomName = "Typogy1",Icon=UIImage.FromBundle("Images/ImageEditor/ITypogy1.png"),IconHeight=70 },
new CustomToolbarItem(){CustomName = "Typogy2",Icon=UIImage.FromBundle("Images/ImageEditor/ITypogy2.png"),IconHeight=70 },
new CustomToolbarItem(){CustomName = "Typogy3",Icon=UIImage.FromBundle("Images/ImageEditor/ITypogy3.png"),IconHeight=70 },
new CustomToolbarItem(){CustomName = "Typogy4",Icon=UIImage.FromBundle("Images/ImageEditor/ITypogy4.png"),IconHeight=70 },
new CustomToolbarItem(){CustomName = "Typogy5",Icon=UIImage.FromBundle("Images/ImageEditor/ITypogy5.png"),IconHeight=70 },
new CustomToolbarItem(){CustomName = "Typogy6",Icon=UIImage.FromBundle("Images/ImageEditor/ITypogy6.png"),IconHeight=70 },
};
// Add the custom tool bar items
var item1 = new FooterToolbarItem()
{
Text = "Add",
Icon = UIImage.FromBundle("Images/ImageEditor/AddIcon.png"),
SubItems = CustomViewItems,
TextHeight = 20,
};
var item2 = new FooterToolbarItem()
{
Text = "Replace",
Icon = UIImage.FromBundle("Images/ImageEditor/ReplaceIcon.png"),
SubItems = CustomViewItems,
TextHeight = 20
};
var item3 = new FooterToolbarItem()
{
Icon = UIImage.FromBundle("Images/ImageEditor/BringFrontIcon"),
Text = "Bring Front",
TextHeight = 20
};
var item4 = new FooterToolbarItem()
{
Icon = UIImage.FromBundle("Images/ImageEditor/SendBack"),
Text = "Send Back",
TextHeight = 20
};
imageEditor.ToolBarSettings.ToolbarItems.Add(item1);
imageEditor.ToolBarSettings.ToolbarItems.Add(item2);
imageEditor.ToolBarSettings.ToolbarItems.Add(item3);
imageEditor.ToolBarSettings.ToolbarItems.Add(item4);
imageEditor.ToolBarSettings.SubItemToolbarHeight = 70;
imageEditor.ItemSelected += ImageEditor_ItemSelected;
imageEditor.ToolBarSettings.ToolbarItemSelected += OnToolbarItemSelected;
imageEditor.Image = image;
this.View.AddSubview(imageEditor);
}
private void ImageEditor_ItemSelected(object sender, ItemSelectedEventArgs args)
{
if (args.Settings != null && args.Settings is CustomViewSettings)
{
customViewSettings = args.Settings as CustomViewSettings;
}
}
private void OnToolbarItemSelected(object sender, ToolbarItemSelectedEventArgs e)
{
string text = string.Empty; //e.ToolbarItem.Name;
if (text != "back")
{
if (e.ToolbarItem is FooterToolbarItem)
{
text = e.ToolbarItem.Text;
e.MoveSubItemsToFooterToolbar = true;
}
else if (e.ToolbarItem is CustomToolbarItem)
text = (e.ToolbarItem as CustomToolbarItem).CustomName;
else
text = e.ToolbarItem.Text;
if (text == "Replace")
{
isReplaced = true;
//imageEditor.ToolbarSettings.FooterToolbarHeight = 70;
}
else if (text == "back")
{
isReplaced = false;
//imageEditor.ToolbarSettings.FooterToolbarHeight = 50;
}
else if (text == "Add")
{
isReplaced = false;
//imageEditor.ToolbarSettings.FooterToolbarHeight = 70;
}
if (isReplaced && imageEditor.IsSelected && (text == "Typogy1" ||
text == "Typogy2" || text == "Typogy3" || text == "Typogy4" || text == "Typogy5" || text == "Typogy6"))
{
imageEditor.Delete();
AddImage(text);
}
else if (text == "Typogy1" ||
text == "Typogy2" || text == "Typogy3" || text == "Typogy4" || text == "Typogy5" || text == "Typogy6")
AddImage(text);
if (text == "Bring Front")
imageEditor.BringToFront();
else if (text == "Send Back")
imageEditor.SendToBack();
}
else
{
isReplaced = false;
}
}
private void AddImage(string text)
{
imageName = text;
var path = NSBundle.MainBundle.PathForResource("Images/ImageEditor/I" + imageName, "png");
UIImage image = new UIImage(path);
UIImageView imageView = new UIImageView();
imageView.Image = image;
imageView.Frame = new CGRect(0, 0, 150, 150);
if (isReplaced)
{
if(customViewSettings!=null)
imageEditor.AddCustomView(imageView, new CustomViewSettings() { Bounds = customViewSettings.Bounds });
}
else
{
imageEditor.AddCustomView(imageView, new CustomViewSettings() { });
}
}
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;
}
}
public class CustomToolbarItem : ToolbarItem
{
public string CustomName { get; set; }
public CustomToolbarItem()
{
}
}
}