BGBeautyView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //
  2. // BGBeautyView.m
  3. // BuguLive
  4. //
  5. // Created by xfg on 2017/2/17.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "BGBeautyView.h"
  9. @interface BGBeautyView()<UIPickerViewDataSource,UIPickerViewDelegate,UIGestureRecognizerDelegate>
  10. {
  11. NSInteger _curIdx;
  12. NSArray * _effectNames;
  13. NSInteger _curEffectIdx;
  14. }
  15. @end
  16. @implementation BGBeautyView
  17. - (id)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self)
  21. {
  22. _beautyBgView = [[UIView alloc]init];
  23. _beautyBgView.backgroundColor = kWhiteColor;
  24. [self addSubview:_beautyBgView];
  25. _filterGroupType = [self addSegCtrlWithItems:
  26. @[ ASLocalizedString(@"关"),
  27. ASLocalizedString(@"简单美颜"),
  28. ASLocalizedString(@"美颜pro"),
  29. ASLocalizedString(@"红润美颜"),
  30. ASLocalizedString(@"美颜特效"),
  31. ]];
  32. _filterGroupType.selectedSegmentIndex = 1;
  33. [_filterGroupType setBackgroundColor:kWhiteColor];
  34. _filterGroupType.tintColor = kAppGrayColor1;
  35. _effectNames = [NSArray arrayWithObjects: ASLocalizedString(@"1 小清新"), ASLocalizedString(@"2 靓丽"),
  36. ASLocalizedString(@"3 甜美可人"), ASLocalizedString(@"4 怀旧"), ASLocalizedString(@"5 蓝调"), ASLocalizedString(@"6 老照片"), nil];
  37. _curEffectIdx = 1;
  38. // 修改美颜参数
  39. _filterParam1 = [self addSliderName:ASLocalizedString(@"参数")From:0 To:100 Init:50];
  40. _filterParam2 = [self addSliderName:ASLocalizedString(@"美白")From:0 To:100 Init:50];
  41. _filterParam3 = [self addSliderName:ASLocalizedString(@"红润")From:0 To:100 Init:50];
  42. _filterParam1.hidden = YES;
  43. _filterParam2.hidden = YES;
  44. _effectPicker = [[UIPickerView alloc] init];
  45. [_beautyBgView addSubview:_effectPicker];
  46. _effectPicker.hidden = YES;
  47. _effectPicker.delegate = self;
  48. _effectPicker.dataSource = self;
  49. _effectPicker.showsSelectionIndicator= YES;
  50. [self selectFilter:1];
  51. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
  52. tap.delegate = self;
  53. [self addGestureRecognizer:tap];
  54. }
  55. return self;
  56. }
  57. - (void)layoutUI
  58. {
  59. _beautyBgView.frame = CGRectMake(0, self.frame.size.height-300, self.frame.size.width, 300);
  60. [self layoutMyCommponent];
  61. }
  62. - (void)layoutMyCommponent
  63. {
  64. CGRect comFrame = _beautyBgView.frame;
  65. _filterGroupType.frame = CGRectMake(kDefaultMargin, comFrame.size.height-40, comFrame.size.width-kDefaultMargin*2, 29);
  66. _filterParam3.frame = CGRectMake(kDefaultMargin, CGRectGetMinY(_filterGroupType.frame)-40, comFrame.size.width-kDefaultMargin*2, 35);
  67. _filterParam2.frame = CGRectMake(kDefaultMargin, CGRectGetMinY(_filterParam3.frame)-40, comFrame.size.width-kDefaultMargin*2, 35);
  68. _filterParam1.frame = CGRectMake(kDefaultMargin, CGRectGetMinY(_filterParam2.frame)-40, comFrame.size.width-kDefaultMargin*2, 35);
  69. _effectPicker.frame = CGRectMake(kDefaultMargin, CGRectGetMinY(_filterParam1.frame)-170, comFrame.size.width-kDefaultMargin*2, 160);
  70. }
  71. - (void)onSegCtrl:(id)sender
  72. {
  73. if (_filterGroupType == sender)
  74. {
  75. [self selectFilter:_filterGroupType.selectedSegmentIndex];
  76. }
  77. }
  78. - (void)selectFilter:(NSInteger)idx
  79. {
  80. if (idx == _curIdx)
  81. {
  82. return;
  83. }
  84. _curIdx = idx;
  85. _filterParam1.hidden = YES;
  86. _filterParam2.hidden = YES;
  87. _filterParam3.hidden = YES;
  88. _effectPicker.hidden = YES;
  89. // 标识当前被选择的滤镜
  90. if (idx == 0)
  91. {
  92. _curFilter = nil;
  93. _beautyBgView.frame = CGRectMake(0, self.frame.size.height-51, self.frame.size.width, 51);
  94. [self layoutMyCommponent];
  95. }
  96. else if (idx == 1)
  97. {
  98. _filterParam3.nameL.text = ASLocalizedString(@"参数");
  99. _filterParam3.hidden = NO;
  100. _curFilter = [[KSYGPUBeautifyExtFilter alloc] init];
  101. _beautyBgView.frame = CGRectMake(0, self.frame.size.height-91, self.frame.size.width, 91);
  102. [self layoutMyCommponent];
  103. }
  104. else if (idx == 2)
  105. { // 美颜pro
  106. KSYBeautifyProFilter * f = [[KSYBeautifyProFilter alloc] init];
  107. _filterParam1.hidden = NO;
  108. _filterParam2.hidden = NO;
  109. _filterParam3.hidden = NO;
  110. _filterParam1.nameL.text = ASLocalizedString(@"磨皮");
  111. f.grindRatio = _filterParam1.normalValue;
  112. f.whitenRatio = _filterParam2.normalValue;
  113. f.ruddyRatio = _filterParam3.normalValue;
  114. _curFilter = f;
  115. _beautyBgView.frame = CGRectMake(0, self.frame.size.height-160, self.frame.size.width, 160);
  116. [self layoutMyCommponent];
  117. }
  118. else if (idx == 3)
  119. { // 红润 + 美颜
  120. _filterParam1.nameL.text = ASLocalizedString(@"磨皮");
  121. _filterParam3.nameL.text = ASLocalizedString(@"红润");
  122. _filterParam1.hidden = NO;
  123. _filterParam2.hidden = NO;
  124. _filterParam3.hidden = NO;
  125. UIImage * rubbyMat = [[self class] KSYGPUImageNamed:@"3_tianmeikeren.png"];
  126. KSYBeautifyFaceFilter * bf = [[KSYBeautifyFaceFilter alloc] initWithRubbyMaterial:rubbyMat];
  127. bf.grindRatio = _filterParam1.normalValue;
  128. bf.whitenRatio = _filterParam2.normalValue;
  129. bf.ruddyRatio = _filterParam3.normalValue;
  130. _curFilter = bf;
  131. _beautyBgView.frame = CGRectMake(0, self.frame.size.height-160, self.frame.size.width, 160);
  132. [self layoutMyCommponent];
  133. }
  134. else if (idx == 4)
  135. { // 美颜 + 特效 滤镜组合
  136. _filterParam1.nameL.text = ASLocalizedString(@"磨皮");
  137. _filterParam3.nameL.text = ASLocalizedString(@"特效");
  138. _filterParam1.hidden = NO;
  139. _filterParam2.hidden = NO;
  140. _filterParam3.hidden = NO;
  141. _effectPicker.hidden = NO;
  142. // 构造美颜滤镜 和 特效滤镜
  143. KSYBeautifyFaceFilter * bf = [[KSYBeautifyFaceFilter alloc] init];
  144. KSYBuildInSpecialEffects * sf = [[KSYBuildInSpecialEffects alloc] initWithIdx:_curEffectIdx];
  145. bf.grindRatio = _filterParam1.normalValue;
  146. bf.whitenRatio = _filterParam2.normalValue;
  147. sf.intensity = _filterParam3.normalValue;
  148. [bf addTarget:sf];
  149. // 用滤镜组 将 滤镜 串联成整体
  150. GPUImageFilterGroup * fg = [[GPUImageFilterGroup alloc] init];
  151. [fg addFilter:bf];
  152. [fg addFilter:sf];
  153. [fg setInitialFilters:[NSArray arrayWithObject:bf]];
  154. [fg setTerminalFilter:sf];
  155. _curFilter = fg;
  156. _beautyBgView.frame = CGRectMake(0, self.frame.size.height-330, self.frame.size.width, 330);
  157. [self layoutMyCommponent];
  158. }
  159. else
  160. {
  161. _curFilter = nil;
  162. }
  163. }
  164. - (void)onSlider:(id)sender
  165. {
  166. if (sender != _filterParam1 &&
  167. sender != _filterParam2 &&
  168. sender != _filterParam3 ) {
  169. return;
  170. }
  171. float nalVal = _filterParam1.normalValue;
  172. if (_curIdx == 1)
  173. {
  174. int val = (nalVal*5) + 1; // level 1~5
  175. [(KSYGPUBeautifyExtFilter *)_curFilter setBeautylevel: val];
  176. }
  177. else if (_curIdx == 2 )
  178. {
  179. KSYBeautifyProFilter * f =(KSYBeautifyProFilter*)_curFilter;
  180. if (sender == _filterParam1 )
  181. {
  182. f.grindRatio = _filterParam1.normalValue;
  183. }
  184. if (sender == _filterParam2 )
  185. {
  186. f.whitenRatio = _filterParam2.normalValue;
  187. }
  188. if (sender == _filterParam3 )
  189. { // 红润参数
  190. f.ruddyRatio = _filterParam3.normalValue;
  191. }
  192. }
  193. else if (_curIdx == 3 )
  194. { // 美颜
  195. KSYBeautifyFaceFilter * f =(KSYBeautifyFaceFilter*)_curFilter;
  196. if (sender == _filterParam1 )
  197. {
  198. f.grindRatio = _filterParam1.normalValue;
  199. }
  200. if (sender == _filterParam2 )
  201. {
  202. f.whitenRatio = _filterParam2.normalValue;
  203. }
  204. if (sender == _filterParam3 )
  205. { // 红润参数
  206. f.ruddyRatio = _filterParam3.normalValue;
  207. }
  208. }
  209. else if ( _curIdx == 4 )
  210. {
  211. GPUImageFilterGroup *fg = (GPUImageFilterGroup *)_curFilter;
  212. KSYBeautifyFaceFilter * bf = (KSYBeautifyFaceFilter *)[fg filterAtIndex:0];
  213. KSYBuildInSpecialEffects * sf = (KSYBuildInSpecialEffects *)[fg filterAtIndex:1];
  214. if (sender == _filterParam1 )
  215. {
  216. bf.grindRatio = _filterParam1.normalValue;
  217. }
  218. if (sender == _filterParam2 )
  219. {
  220. bf.whitenRatio = _filterParam2.normalValue;
  221. }
  222. if (sender == _filterParam3 )
  223. { // 特效参数
  224. [sf setIntensity:_filterParam3.normalValue];
  225. }
  226. }
  227. }
  228. #pragma mark - ----------------------- effect picker -----------------------
  229. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
  230. {
  231. return 1; // 单列
  232. }
  233. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  234. {
  235. return _effectNames.count;//
  236. }
  237. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
  238. {
  239. return [_effectNames objectAtIndex:row];
  240. }
  241. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  242. {
  243. _curEffectIdx = row+1;
  244. if ( [_curFilter isMemberOfClass:[GPUImageFilterGroup class]])
  245. {
  246. GPUImageFilterGroup * fg = (GPUImageFilterGroup *)_curFilter;
  247. KSYBuildInSpecialEffects * sf = (KSYBuildInSpecialEffects *)[fg filterAtIndex:1];
  248. [sf setSpecialEffectsIdx: _curEffectIdx];
  249. }
  250. }
  251. #pragma mark - ----------------------- 单击 -----------------------
  252. - (void)tapClick:(UITapGestureRecognizer *)tap
  253. {
  254. self.hidden = YES;
  255. }
  256. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  257. {
  258. if ([touch.view isDescendantOfView:_beautyBgView])
  259. {
  260. return NO;
  261. }
  262. return YES;
  263. }
  264. #pragma mark - ----------------------- UI -----------------------
  265. - (UISegmentedControl *)addSegCtrlWithItems:(NSArray *)items
  266. {
  267. UISegmentedControl *segC = [[UISegmentedControl alloc] initWithItems:items];
  268. segC.selectedSegmentIndex = 0;
  269. segC.layer.cornerRadius = 5;
  270. segC.backgroundColor = [UIColor lightGrayColor];
  271. [segC addTarget:self action:@selector(onSegCtrl:) forControlEvents:UIControlEventValueChanged];
  272. [_beautyBgView addSubview:segC];
  273. return segC;
  274. }
  275. - (BGNameSlider *)addSliderName: (NSString*) name
  276. From: (float) minV
  277. To: (float) maxV
  278. Init: (float) iniV {
  279. BGNameSlider *sl = [[BGNameSlider alloc] init];
  280. [_beautyBgView addSubview:sl];
  281. sl.slider.minimumValue = minV;
  282. sl.slider.maximumValue = maxV;
  283. sl.slider.value = iniV;
  284. sl.nameL.text = name;
  285. sl.normalValue = (iniV -minV)/maxV;
  286. sl.valueL.text = [NSString stringWithFormat:@"%d", (int)iniV];
  287. if (iniV <2){
  288. sl.precision = 2;
  289. }
  290. [sl.slider addTarget:self action:@selector(onSlider:) forControlEvents:UIControlEventValueChanged ];
  291. __weak typeof(self) ws = self;
  292. sl.onSliderBlock = ^(id sender){
  293. [ws onSlider:sender];
  294. };
  295. return sl;
  296. }
  297. - (UILabel *)addLable:(NSString*)title
  298. {
  299. UILabel * lbl = [[UILabel alloc] init];
  300. lbl.text = title;
  301. [_beautyBgView addSubview:lbl];
  302. lbl.backgroundColor = [UIColor colorWithWhite:0.8 alpha:0.3];
  303. return lbl;
  304. }
  305. #pragma mark - load resource from resource bundle
  306. + (NSBundle*)KSYGPUResourceBundle
  307. {
  308. static dispatch_once_t onceToken;
  309. static NSBundle *resBundle = nil;
  310. dispatch_once(&onceToken, ^{
  311. resBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"KSYGPUResource" withExtension:@"bundle"]];
  312. });
  313. return resBundle;
  314. }
  315. + (UIImage*)KSYGPUImageNamed:(NSString*)name
  316. {
  317. UIImage *imageFromMainBundle = [UIImage imageNamed:name];
  318. if (imageFromMainBundle)
  319. {
  320. return imageFromMainBundle;
  321. }
  322. UIImage *imageFromKSYBundle = [UIImage imageWithContentsOfFile:[[[BGBeautyView KSYGPUResourceBundle] resourcePath] stringByAppendingPathComponent:name]];
  323. return imageFromKSYBundle;
  324. }
  325. @end