PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/VideoEffects/ViewController.m

https://gitlab.com/Mr.Tomato/VideoEffects
Objective C | 1214 lines | 995 code | 178 blank | 41 comment | 97 complexity | 4d7186c0ef456330832d222bb2a87b8b MD5 | raw file
  1. //
  2. // ViewController.m
  3. // VideoEffects
  4. //
  5. // Created by Johnny Xu(徐景周) on 7/22/15.
  6. // Copyright (c) 2015 Future Studio. All rights reserved.
  7. //
  8. #import <AssetsLibrary/AssetsLibrary.h>
  9. #import <StoreKit/StoreKit.h>
  10. #import "ViewController.h"
  11. #import "PBJVideoPlayerController.h"
  12. #import "CaptureViewController.h"
  13. #import "JGActionSheet.h"
  14. #import "DBPrivateHelperController.h"
  15. #import "KGModal.h"
  16. #import "CMPopTipView.h"
  17. #import "UIAlertView+Blocks.h"
  18. #import "ExportEffects.h"
  19. #import "NSString+Height.h"
  20. #import "AudioViewController.h"
  21. #import "IASKAppSettingsViewController.h"
  22. #define MaxVideoLength MAX_VIDEO_DUR
  23. #define DemoVideoName @"Demo.mp4"
  24. typedef NS_ENUM(NSUInteger, FilterType)
  25. {
  26. kDistortion = 0,
  27. kSwirl,
  28. kMosaic,
  29. };
  30. @interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate, PBJVideoPlayerControllerDelegate, SKStoreProductViewControllerDelegate, IASKSettingsDelegate, UIPopoverControllerDelegate>
  31. {
  32. CMPopTipView *_popTipView;
  33. }
  34. @property (nonatomic, strong) PBJVideoPlayerController *demoVideoPlayerController;
  35. @property (nonatomic, strong) UIView *demoVideoContentView;
  36. @property (nonatomic, strong) UIImageView *demoPlayButton;
  37. @property (nonatomic, strong) UIScrollView *captureContentView;
  38. @property (nonatomic, strong) UIButton *videoView;
  39. @property (nonatomic, strong) UIScrollView *videoContentView;
  40. @property (nonatomic, strong) PBJVideoPlayerController *videoPlayerController;
  41. @property (nonatomic, strong) UIImageView *playButton;
  42. @property (nonatomic, strong) UIButton *closeVideoPlayerButton;
  43. @property (nonatomic, copy) NSURL *videoPickURL;
  44. @property (nonatomic, copy) NSString *audioPickFile;
  45. @property (nonatomic, strong) UIView *parentView;
  46. @property (nonatomic, strong) UIButton *demoButton;
  47. @property (nonatomic, strong) IASKAppSettingsViewController *appSettingsViewController;
  48. @property (nonatomic, strong) UIPopoverController* currentPopoverController;
  49. @end
  50. @implementation ViewController
  51. #pragma mark - Authorization Helper
  52. - (void)popupAlertView
  53. {
  54. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:GBLocalizedString(@"Private_Setting_Audio_Tips") delegate:nil cancelButtonTitle:GBLocalizedString(@"IKnow") otherButtonTitles:nil, nil];
  55. [alertView show];
  56. }
  57. - (void)popupAuthorizationHelper:(id)type
  58. {
  59. DBPrivateHelperController *privateHelper = [DBPrivateHelperController helperForType:[type longValue]];
  60. privateHelper.snapshot = [self snapshot];
  61. privateHelper.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
  62. [self presentViewController:privateHelper animated:YES completion:nil];
  63. }
  64. - (UIImage *)snapshot
  65. {
  66. id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
  67. UIGraphicsBeginImageContextWithOptions(appDelegate.window.bounds.size, NO, appDelegate.window.screen.scale);
  68. [appDelegate.window drawViewHierarchyInRect:appDelegate.window.bounds afterScreenUpdates:NO];
  69. UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
  70. UIGraphicsEndImageContext();
  71. return snapshotImage;
  72. }
  73. #pragma mark - File Helper
  74. - (AVURLAsset *)getURLAsset:(NSString *)filePath
  75. {
  76. NSURL *videoURL = getFileURL(filePath);
  77. AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
  78. return asset;
  79. }
  80. #pragma mark - Delete Temp Files
  81. - (void)deleteTempDirectory
  82. {
  83. NSString *dir = NSTemporaryDirectory();
  84. deleteFilesAt(dir, @"mov");
  85. }
  86. #pragma mark - Custom ActionSheet
  87. - (void)showCustomActionSheetByView:(UIView *)anchor
  88. {
  89. UIView *locationAnchor = anchor;
  90. NSString *videoTitle = [NSString stringWithFormat:@"%@", GBLocalizedString(@"SelectVideo")];
  91. JGActionSheetSection *sectionVideo = [JGActionSheetSection sectionWithTitle:videoTitle
  92. message:nil
  93. buttonTitles:@[
  94. GBLocalizedString(@"Camera"),
  95. GBLocalizedString(@"PhotoAlbum")
  96. ]
  97. buttonStyle:JGActionSheetButtonStyleDefault];
  98. [sectionVideo setButtonStyle:JGActionSheetButtonStyleBlue forButtonAtIndex:0];
  99. [sectionVideo setButtonStyle:JGActionSheetButtonStyleBlue forButtonAtIndex:1];
  100. NSArray *sections = (iPad ? @[sectionVideo] : @[sectionVideo, [JGActionSheetSection sectionWithTitle:nil message:nil buttonTitles:@[GBLocalizedString(@"Cancel")] buttonStyle:JGActionSheetButtonStyleCancel]]);
  101. JGActionSheet *sheet = [[JGActionSheet alloc] initWithSections:sections];
  102. [sheet setButtonPressedBlock:^(JGActionSheet *sheet, NSIndexPath *indexPath)
  103. {
  104. NSLog(@"indexPath: %ld; section: %ld", (long)indexPath.row, (long)indexPath.section);
  105. if (indexPath.section == 0)
  106. {
  107. if (indexPath.row == 0)
  108. {
  109. // Check permission for Video & Audio
  110. [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted)
  111. {
  112. if (!granted)
  113. {
  114. [self performSelectorOnMainThread:@selector(popupAlertView) withObject:nil waitUntilDone:YES];
  115. return;
  116. }
  117. else
  118. {
  119. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted)
  120. {
  121. if (!granted)
  122. {
  123. [self performSelectorOnMainThread:@selector(popupAuthorizationHelper:) withObject:[NSNumber numberWithLong:DBPrivacyTypeCamera] waitUntilDone:YES];
  124. return;
  125. }
  126. else
  127. {
  128. // Has permisstion
  129. [self performSelectorOnMainThread:@selector(pickBackgroundVideoFromCamera) withObject:nil waitUntilDone:NO];
  130. }
  131. }];
  132. }
  133. }];
  134. }
  135. else if (indexPath.row == 1)
  136. {
  137. // Check permisstion for photo album
  138. ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
  139. if (authStatus == ALAuthorizationStatusRestricted || authStatus == ALAuthorizationStatusDenied)
  140. {
  141. [self performSelectorOnMainThread:@selector(popupAuthorizationHelper:) withObject:[NSNumber numberWithLong:DBPrivacyTypePhoto] waitUntilDone:YES];
  142. return;
  143. }
  144. else
  145. {
  146. // Has permisstion to execute
  147. [self performSelector:@selector(pickBackgroundVideoFromPhotosAlbum) withObject:nil afterDelay:0.1];
  148. }
  149. }
  150. }
  151. [sheet dismissAnimated:YES];
  152. }];
  153. if (iPad)
  154. {
  155. [sheet setOutsidePressBlock:^(JGActionSheet *sheet)
  156. {
  157. [sheet dismissAnimated:YES];
  158. }];
  159. CGPoint point = (CGPoint){ CGRectGetMidX(locationAnchor.bounds), CGRectGetMaxY(locationAnchor.bounds) };
  160. point = [self.navigationController.view convertPoint:point fromView:locationAnchor];
  161. [sheet showFromPoint:point inView:self.navigationController.view arrowDirection:JGActionSheetArrowDirectionTop animated:YES];
  162. }
  163. else
  164. {
  165. [sheet setOutsidePressBlock:^(JGActionSheet *sheet)
  166. {
  167. [sheet dismissAnimated:YES];
  168. }];
  169. [sheet showInView:self.navigationController.view animated:YES];
  170. }
  171. }
  172. - (void)showCustomActionSheetByNav:(UIBarButtonItem *)barButtonItem withEvent:(UIEvent *)event
  173. {
  174. UIView *anchor = [event.allTouches.anyObject view];
  175. [self showCustomActionSheetByView:anchor];
  176. }
  177. #pragma mark - appSettingsViewController
  178. - (IASKAppSettingsViewController*)appSettingsViewController
  179. {
  180. if (!_appSettingsViewController)
  181. {
  182. _appSettingsViewController = [[IASKAppSettingsViewController alloc] init];
  183. _appSettingsViewController.delegate = self;
  184. }
  185. return _appSettingsViewController;
  186. }
  187. - (void)showSettingsModal:(id)sender
  188. {
  189. if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad)
  190. {
  191. [self showSettingsPopover:sender];
  192. }
  193. else
  194. {
  195. [self.navigationController pushViewController:self.appSettingsViewController animated:YES];
  196. }
  197. }
  198. - (void)showSettingsPopover:(id)sender
  199. {
  200. if(self.currentPopoverController)
  201. {
  202. [self dismissCurrentPopover];
  203. return;
  204. }
  205. self.appSettingsViewController.showDoneButton = NO;
  206. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.appSettingsViewController];
  207. UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
  208. popover.delegate = self;
  209. [popover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
  210. self.currentPopoverController = popover;
  211. }
  212. - (void) dismissCurrentPopover
  213. {
  214. [self.currentPopoverController dismissPopoverAnimated:YES];
  215. self.currentPopoverController = nil;
  216. }
  217. #pragma mark - UIPopoverControllerDelegate
  218. - (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
  219. {
  220. self.currentPopoverController = nil;
  221. }
  222. #pragma mark IASKAppSettingsViewControllerDelegate
  223. - (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController*)sender
  224. {
  225. [self.navigationController popViewControllerAnimated:YES];
  226. }
  227. #pragma mark - PBJVideoPlayerControllerDelegate
  228. - (void)videoPlayerReady:(PBJVideoPlayerController *)videoPlayer
  229. {
  230. //NSLog(@"Max duration of the video: %f", videoPlayer.maxDuration);
  231. }
  232. - (void)videoPlayerPlaybackStateDidChange:(PBJVideoPlayerController *)videoPlayer
  233. {
  234. }
  235. - (void)videoPlayerPlaybackWillStartFromBeginning:(PBJVideoPlayerController *)videoPlayer
  236. {
  237. if (videoPlayer == _videoPlayerController)
  238. {
  239. _playButton.alpha = 1.0f;
  240. _playButton.hidden = NO;
  241. [UIView animateWithDuration:0.1f animations:^{
  242. _playButton.alpha = 0.0f;
  243. } completion:^(BOOL finished)
  244. {
  245. _playButton.hidden = YES;
  246. }];
  247. }
  248. else if (videoPlayer == _demoVideoPlayerController)
  249. {
  250. _demoPlayButton.alpha = 1.0f;
  251. _demoPlayButton.hidden = NO;
  252. [UIView animateWithDuration:0.1f animations:^{
  253. _demoPlayButton.alpha = 0.0f;
  254. } completion:^(BOOL finished)
  255. {
  256. _demoPlayButton.hidden = YES;
  257. }];
  258. }
  259. }
  260. - (void)videoPlayerPlaybackDidEnd:(PBJVideoPlayerController *)videoPlayer
  261. {
  262. if (videoPlayer == _videoPlayerController)
  263. {
  264. _playButton.hidden = NO;
  265. [UIView animateWithDuration:0.1f animations:^{
  266. _playButton.alpha = 1.0f;
  267. } completion:^(BOOL finished)
  268. {
  269. }];
  270. }
  271. else if (videoPlayer == _demoVideoPlayerController)
  272. {
  273. _demoPlayButton.hidden = NO;
  274. [UIView animateWithDuration:0.1f animations:^{
  275. _demoPlayButton.alpha = 1.0f;
  276. } completion:^(BOOL finished)
  277. {
  278. }];
  279. }
  280. }
  281. #pragma mark - UIImagePickerControllerDelegate
  282. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  283. {
  284. // 1.
  285. [self dismissViewControllerAnimated:NO completion:nil];
  286. NSLog(@"info = %@",info);
  287. // 2.
  288. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  289. if([mediaType isEqualToString:@"public.movie"])
  290. {
  291. NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
  292. [self setPickedVideo:url];
  293. }
  294. else
  295. {
  296. NSLog(@"Error media type");
  297. return;
  298. }
  299. }
  300. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  301. {
  302. [picker dismissViewControllerAnimated:NO completion:nil];
  303. }
  304. - (void)setPickedVideo:(NSURL *)url
  305. {
  306. [self setPickedVideo:url checkVideoLength:YES];
  307. }
  308. - (void)setPickedVideo:(NSURL *)url checkVideoLength:(BOOL)checkVideoLength
  309. {
  310. if (!url || (url && ![url isFileURL]))
  311. {
  312. NSLog(@"Input video url is invalid.");
  313. return;
  314. }
  315. if (checkVideoLength)
  316. {
  317. if (getVideoDuration(url) > MaxVideoLength)
  318. {
  319. NSString *ok = GBLocalizedString(@"OK");
  320. NSString *error = GBLocalizedString(@"Reminder");
  321. NSString *fileLenHint = GBLocalizedString(@"FileLenHint");
  322. NSString *seconds = GBLocalizedString(@"Seconds");
  323. NSString *hint = [fileLenHint stringByAppendingFormat:@" %.0f ", MaxVideoLength];
  324. hint = [hint stringByAppendingString:seconds];
  325. UIAlertView* alert = [[UIAlertView alloc] initWithTitle:error
  326. message:hint
  327. delegate:nil
  328. cancelButtonTitle:ok
  329. otherButtonTitles: nil];
  330. [alert show];
  331. return;
  332. }
  333. }
  334. _videoPickURL = url;
  335. NSLog(@"Pick background video is success: %@", _videoPickURL);
  336. [self reCalcVideoSize:[url relativePath]];
  337. // Setting
  338. [self defaultVideoSetting:url];
  339. // Hint to next step
  340. if ([self getAppRunCount] < 6 && [self getNextStepRunCondition])
  341. {
  342. if (_popTipView)
  343. {
  344. NSString *hint = GBLocalizedString(@"UsageNextHint");
  345. _popTipView.message = hint;
  346. [_popTipView autoDismissAnimated:YES atTimeInterval:5.0];
  347. [_popTipView presentPointingAtBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];
  348. }
  349. }
  350. }
  351. #pragma mark - pickBackgroundVideoFromPhotosAlbum
  352. - (void)pickBackgroundVideoFromPhotosAlbum
  353. {
  354. [self pickVideoFromPhotoAlbum];
  355. }
  356. - (void)pickVideoFromPhotoAlbum
  357. {
  358. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  359. picker.delegate = self;
  360. picker.allowsEditing = YES;
  361. picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
  362. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  363. {
  364. // Only movie
  365. NSArray* availableMedia = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
  366. picker.mediaTypes = [NSArray arrayWithObject:availableMedia[1]];
  367. }
  368. [self presentViewController:picker animated:YES completion:nil];
  369. }
  370. #pragma mark - pickBackgroundVideoFromCamera
  371. - (void)pickBackgroundVideoFromCamera
  372. {
  373. [self pickVideoFromCamera];
  374. }
  375. - (void)pickVideoFromCamera
  376. {
  377. CaptureViewController *captureVC = [[CaptureViewController alloc] init];
  378. [captureVC setCallback:^(BOOL success, id result)
  379. {
  380. if (success)
  381. {
  382. NSURL *fileURL = result;
  383. [self setPickedVideo:fileURL checkVideoLength:NO];
  384. }
  385. else
  386. {
  387. NSLog(@"Video Picker Failed: %@", result);
  388. }
  389. }];
  390. [self presentViewController:captureVC animated:YES completion:^{
  391. NSLog(@"PickVideo present");
  392. }];
  393. }
  394. #pragma mark - pickMusicFromCustom
  395. - (void)pickMusicFromCustom
  396. {
  397. if (![self getNextStepRunCondition])
  398. {
  399. NSString *message = nil;
  400. message = GBLocalizedString(@"VideoIsEmptyHint");
  401. showAlertMessage(message, nil);
  402. return;
  403. }
  404. AudioViewController *audioController = [[AudioViewController alloc] init];
  405. [audioController setSeletedRowBlock: ^(BOOL success, id result) {
  406. if (success && [result isKindOfClass:[NSNumber class]])
  407. {
  408. NSInteger index = [result integerValue];
  409. NSLog(@"pickAudio result: %ld", (long)index);
  410. if (index != NSNotFound)
  411. {
  412. NSArray *allAudios = [NSArray arrayWithObjects:
  413. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"Apple"), @"song", @"Apple.mp3", @"url", nil],
  414. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"TheMoodOfLove"), @"song", @"Love Paradise.mp3", @"url", nil],
  415. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"LeadMeOn"), @"song", @"Lead Me On.mp3", @"url", nil],
  416. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"Butterfly"), @"song", @"Butterfly.mp3", @"url", nil],
  417. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"PenguinsGame"), @"song", @"Penguin's Game.mp3", @"url", nil],
  418. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"ALittleKiss"), @"song", @"A Little Kiss.mp3", @"url", nil],
  419. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"ByeByeSunday"), @"song", @"Bye Bye Sunday.mp3", @"url", nil],
  420. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"ComeWithMe"), @"song", @"Come With Me.mp3", @"url", nil],
  421. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"DolphinTango"), @"song", @"Dolphin Tango.mp3", @"url", nil],
  422. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"IDo"), @"song", @"I Do.mp3", @"url", nil],
  423. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"LetMeKnow"), @"song", @"Let Me Know.mp3", @"url", nil],
  424. [NSDictionary dictionaryWithObjectsAndKeys:GBLocalizedString(@"SwingDance"), @"song", @"Swing Dance.mp3", @"url", nil],
  425. nil];
  426. NSDictionary *item = [allAudios objectAtIndex:index];
  427. NSString *file = [item objectForKey:@"url"];
  428. _audioPickFile = file;
  429. }
  430. else
  431. {
  432. _audioPickFile = nil;
  433. }
  434. // Build video effect
  435. FilterType filterType = [self getFilterType];
  436. ThemesType curThemeType = KThemeOldFilm;
  437. if (filterType == kSwirl)
  438. {
  439. curThemeType = kThemeNostalgia;
  440. }
  441. else if (filterType == kMosaic)
  442. {
  443. curThemeType = kThemeMood;
  444. }
  445. [self handleConvert:curThemeType];
  446. }
  447. }];
  448. [self.navigationController pushViewController:audioController animated:NO];
  449. }
  450. #pragma mark - getNextStepCondition
  451. - (BOOL)getNextStepRunCondition
  452. {
  453. BOOL result = TRUE;
  454. if (!_videoPickURL)
  455. {
  456. result = FALSE;
  457. }
  458. return result;
  459. }
  460. #pragma mark - Default Setting
  461. - (void)defaultVideoSetting:(NSURL *)url
  462. {
  463. [self showVideoPlayView:YES];
  464. [self playDemoVideo:[url absoluteString] withinVideoPlayerController:_videoPlayerController];
  465. }
  466. #pragma mark - playDemoVideo
  467. - (void)playDemoVideo:(NSString*)inputVideoPath withinVideoPlayerController:(PBJVideoPlayerController*)videoPlayerController
  468. {
  469. dispatch_async(dispatch_get_main_queue(), ^{
  470. videoPlayerController.videoPath = inputVideoPath;
  471. [videoPlayerController playFromBeginning];
  472. });
  473. }
  474. #pragma mark - StopAllVideo
  475. - (void)stopAllVideo
  476. {
  477. if (_videoPlayerController.playbackState == PBJVideoPlayerPlaybackStatePlaying)
  478. {
  479. [_videoPlayerController stop];
  480. }
  481. }
  482. #pragma mark - Show/Hide
  483. - (void)showVideoPlayView:(BOOL)show
  484. {
  485. if (show)
  486. {
  487. _videoContentView.hidden = NO;
  488. _closeVideoPlayerButton.hidden = NO;
  489. _videoView.hidden = YES;
  490. }
  491. else
  492. {
  493. [self stopAllVideo];
  494. _videoView.hidden = NO;
  495. _videoContentView.hidden = YES;
  496. _closeVideoPlayerButton.hidden = YES;
  497. }
  498. }
  499. #pragma mark - reCalc on the basis of video size & view size
  500. - (void)reCalcVideoSize:(NSString *)videoPath
  501. {
  502. CGFloat statusBarHeight = iOS7AddStatusHeight;
  503. CGSize sizeVideo = [self reCalcVideoViewSize:videoPath];
  504. _videoContentView.frame = CGRectMake(CGRectGetMidX(self.view.frame) - sizeVideo.width/2, CGRectGetMidY(self.view.frame) - sizeVideo.height/2 + statusBarHeight, sizeVideo.width, sizeVideo.height);
  505. _videoPlayerController.view.frame = _videoContentView.bounds;
  506. _playButton.center = _videoPlayerController.view.center;
  507. CGFloat width = 45;
  508. _closeVideoPlayerButton.frame = CGRectMake(CGRectGetMinX(_videoContentView.frame) - width/2, CGRectGetMinY(_videoContentView.frame) - width/2, width, width);
  509. }
  510. - (CGSize)reCalcVideoViewSize:(NSString *)videoPath
  511. {
  512. CGSize resultSize = CGSizeZero;
  513. if (isStringEmpty(videoPath))
  514. {
  515. return resultSize;
  516. }
  517. UIImage *videoFrame = getImageFromVideoFrame(getFileURL(videoPath), kCMTimeZero);
  518. if (!videoFrame || videoFrame.size.height < 1 || videoFrame.size.width < 1)
  519. {
  520. return resultSize;
  521. }
  522. NSLog(@"reCalcVideoViewSize: %@, width: %f, height: %f", videoPath, videoFrame.size.width, videoFrame.size.height);
  523. CGFloat statusBarHeight = 0; //iOS7AddStatusHeight;
  524. CGFloat navHeight = 0; //CGRectGetHeight(self.navigationController.navigationBar.bounds);
  525. CGFloat gap = 15;
  526. CGFloat height = CGRectGetHeight(self.view.frame) - navHeight - statusBarHeight - 2*gap;
  527. CGFloat width = CGRectGetWidth(self.view.frame) - 2*gap;
  528. if (height < width)
  529. {
  530. width = height;
  531. }
  532. else if (height > width)
  533. {
  534. height = width;
  535. }
  536. CGFloat videoHeight = videoFrame.size.height, videoWidth = videoFrame.size.width;
  537. CGFloat scaleRatio = videoHeight/videoWidth;
  538. CGFloat resultHeight = 0, resultWidth = 0;
  539. if (videoHeight <= height && videoWidth <= width)
  540. {
  541. resultHeight = videoHeight;
  542. resultWidth = videoWidth;
  543. }
  544. else if (videoHeight <= height && videoWidth > width)
  545. {
  546. resultWidth = width;
  547. resultHeight = height*scaleRatio;
  548. }
  549. else if (videoHeight > height && videoWidth <= width)
  550. {
  551. resultHeight = height;
  552. resultWidth = width/scaleRatio;
  553. }
  554. else
  555. {
  556. if (videoHeight < videoWidth)
  557. {
  558. resultWidth = width;
  559. resultHeight = height*scaleRatio;
  560. }
  561. else if (videoHeight == videoWidth)
  562. {
  563. resultWidth = width;
  564. resultHeight = height;
  565. }
  566. else
  567. {
  568. resultHeight = height;
  569. resultWidth = width/scaleRatio;
  570. }
  571. }
  572. resultSize = CGSizeMake(resultWidth, resultHeight);
  573. return resultSize;
  574. }
  575. #pragma mark - getOutputFilePath
  576. - (NSString*)getOutputFilePath
  577. {
  578. NSString* mp4OutputFile = [NSTemporaryDirectory() stringByAppendingPathComponent:@"outputMovie.mov"];
  579. return mp4OutputFile;
  580. }
  581. #pragma mark - Progress callback
  582. - (void)retrievingProgress:(id)progress title:(NSString *)text
  583. {
  584. if (progress && [progress isKindOfClass:[NSNumber class]])
  585. {
  586. NSString *title = text ?text :GBLocalizedString(@"SavingVideo");
  587. NSString *currentPrecentage = [NSString stringWithFormat:@"%d%%", (int)([progress floatValue] * 100)];
  588. ProgressBarUpdateLoading(title, currentPrecentage);
  589. }
  590. }
  591. #pragma mark AppStore Open
  592. - (void)showAppInAppStore:(NSString *)appId
  593. {
  594. Class isAllow = NSClassFromString(@"SKStoreProductViewController");
  595. if (isAllow)
  596. {
  597. // > iOS6.0
  598. SKStoreProductViewController *sKStoreProductViewController = [[SKStoreProductViewController alloc] init];
  599. sKStoreProductViewController.delegate = self;
  600. [self presentViewController:sKStoreProductViewController
  601. animated:YES
  602. completion:nil];
  603. [sKStoreProductViewController loadProductWithParameters:@{SKStoreProductParameterITunesItemIdentifier: appId}completionBlock:^(BOOL result, NSError *error)
  604. {
  605. if (error)
  606. {
  607. NSLog(@"%@",error);
  608. }
  609. }];
  610. }
  611. else
  612. {
  613. // < iOS6.0
  614. NSString *appUrl = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/us/app/id%@?mt=8", appId];
  615. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appUrl]];
  616. }
  617. }
  618. - (void)createRecommendAppButtons:(UIView *)containerView
  619. {
  620. // Recommend App
  621. UIButton *beautyTime = [[UIButton alloc] init];
  622. [beautyTime setTitle:GBLocalizedString(@"BeautyTime")
  623. forState:UIControlStateNormal];
  624. UIButton *photoBeautify = [[UIButton alloc] init];
  625. [photoBeautify setTitle:GBLocalizedString(@"PhotoBeautify")
  626. forState:UIControlStateNormal];
  627. [photoBeautify setTag:1];
  628. [beautyTime setTag:2];
  629. CGFloat gap = 0, height = 30, width = 80;
  630. CGFloat fontSize = 16;
  631. NSString *fontName = @"迷你简启体"; // GBLocalizedString(@"FontName");
  632. photoBeautify.frame = CGRectMake(gap, gap, width, height);
  633. [photoBeautify.titleLabel setFont:[UIFont fontWithName:fontName size:fontSize]];
  634. [photoBeautify.titleLabel setTextAlignment:NSTextAlignmentLeft];
  635. [photoBeautify setTitleColor:kLightBlue forState:UIControlStateNormal];
  636. [photoBeautify addTarget:self action:@selector(recommendAppButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  637. beautyTime.frame = CGRectMake(CGRectGetWidth(containerView.frame) - width - gap, gap, width, height);
  638. [beautyTime.titleLabel setFont:[UIFont fontWithName:fontName size:fontSize]];
  639. [beautyTime.titleLabel setTextAlignment:NSTextAlignmentRight];
  640. [beautyTime setTitleColor:kLightBlue forState:UIControlStateNormal];
  641. [beautyTime addTarget:self action:@selector(recommendAppButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  642. [containerView addSubview:photoBeautify];
  643. [containerView addSubview:beautyTime];
  644. }
  645. - (void)recommendAppButtonAction:(id)sender
  646. {
  647. UIButton *button = (UIButton *)sender;
  648. switch (button.tag)
  649. {
  650. case 1:
  651. {
  652. // Picture in Picture
  653. [self showAppInAppStore:@"1006401631"];
  654. break;
  655. }
  656. case 2:
  657. {
  658. // BeautyTime
  659. [self showAppInAppStore:@"1002437952"];
  660. break;
  661. }
  662. default:
  663. break;
  664. }
  665. [button setSelected:YES];
  666. }
  667. #pragma mark - SKStoreProductViewControllerDelegate
  668. // Dismiss contorller
  669. - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
  670. {
  671. [viewController dismissViewControllerAnimated:YES completion:nil];
  672. }
  673. #pragma mark - View LifeCycle
  674. - (void)createRecommendAppView
  675. {
  676. CGFloat statusBarHeight = 0; //iOS7AddStatusHeight;
  677. CGFloat navHeight = 0; //CGRectGetHeight(self.navigationController.navigationBar.bounds);
  678. CGFloat height = 30;
  679. UIView *recommendAppView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame) - height - navHeight - statusBarHeight, CGRectGetWidth(self.view.frame), height)];
  680. [recommendAppView setBackgroundColor:[UIColor clearColor]];
  681. [self.view addSubview:recommendAppView];
  682. [self createRecommendAppButtons:recommendAppView];
  683. // Demo button
  684. CGFloat widthButton = 100, heightButton = 60;
  685. _demoButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)/2 - widthButton/2, CGRectGetHeight(self.view.frame) - heightButton, widthButton, heightButton)];
  686. UIImage *image = [UIImage imageNamed:@"demo"];
  687. [_demoButton setImage:image forState:UIControlStateNormal];
  688. [_demoButton addTarget:self action:@selector(handleDemoButton) forControlEvents:UIControlEventTouchUpInside];
  689. [self.view addSubview:_demoButton];
  690. }
  691. - (void)createVideoView
  692. {
  693. _parentView = [[UIView alloc] initWithFrame:self.view.bounds];
  694. _parentView.backgroundColor = [UIColor clearColor];
  695. [self.view addSubview:_parentView];
  696. [self createContentView:_parentView];
  697. [self createVideoPlayView:_parentView];
  698. }
  699. - (void)createContentView:(UIView *)parentView
  700. {
  701. CGFloat statusBarHeight = 0; //iOS7AddStatusHeight;
  702. CGFloat navHeight = CGRectGetHeight(self.navigationController.navigationBar.bounds);
  703. CGFloat gap = 15, len = MIN((CGRectGetHeight(self.view.frame) - navHeight - statusBarHeight - 2*gap), (CGRectGetWidth(self.view.frame) - navHeight - statusBarHeight - 2*gap));
  704. _captureContentView = [[UIScrollView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - len/2, CGRectGetMidY(self.view.frame) - len/2, len, len)];
  705. [_captureContentView setBackgroundColor:[UIColor clearColor]];
  706. [parentView addSubview:_captureContentView];
  707. _videoView = [[UIButton alloc] initWithFrame:_captureContentView.frame];
  708. [_videoView setBackgroundColor:[UIColor clearColor]];
  709. _videoView.layer.cornerRadius = 5;
  710. _videoView.layer.masksToBounds = YES;
  711. _videoView.layer.borderWidth = 1.0;
  712. _videoView.layer.borderColor = [UIColor whiteColor].CGColor;
  713. UIImage *addFileImage = [UIImage imageNamed:@"Video_Add"];
  714. [_videoView setImage:addFileImage forState:UIControlStateNormal];
  715. [_videoView addTarget:self action:@selector(showCustomActionSheetByView:) forControlEvents:UIControlEventTouchUpInside];
  716. [parentView addSubview:_videoView];
  717. }
  718. - (void)createVideoPlayView:(UIView *)parentView
  719. {
  720. _videoContentView = [[UIScrollView alloc] initWithFrame:_captureContentView.frame];
  721. [_videoContentView setBackgroundColor:[UIColor clearColor]];
  722. [parentView addSubview:_videoContentView];
  723. // Video player
  724. _videoPlayerController = [[PBJVideoPlayerController alloc] init];
  725. _videoPlayerController.delegate = self;
  726. _videoPlayerController.view.frame = _videoView.bounds;
  727. _videoPlayerController.view.clipsToBounds = YES;
  728. [self addChildViewController:_videoPlayerController];
  729. [_videoContentView addSubview:_videoPlayerController.view];
  730. _playButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"play_button"]];
  731. _playButton.center = _videoPlayerController.view.center;
  732. [_videoPlayerController.view addSubview:_playButton];
  733. // Close video player
  734. UIImage *imageClose = [UIImage imageNamed:@"close"];
  735. CGFloat width = 45;
  736. _closeVideoPlayerButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(_videoContentView.frame) - width/2, CGRectGetMinY(_videoContentView.frame) - width/2, width, width)];
  737. _closeVideoPlayerButton.center = _captureContentView.frame.origin;
  738. [_closeVideoPlayerButton setImage:imageClose forState:(UIControlStateNormal)];
  739. [_closeVideoPlayerButton addTarget:self action:@selector(handleCloseVideo:) forControlEvents:UIControlEventTouchUpInside];
  740. [parentView addSubview:_closeVideoPlayerButton];
  741. _closeVideoPlayerButton.hidden = YES;
  742. }
  743. - (void)createNavigationBar
  744. {
  745. NSString *fontName = GBLocalizedString(@"FontName");
  746. CGFloat fontSize = 20;
  747. NSShadow *shadow = [[NSShadow alloc] init];
  748. shadow.shadowColor = [UIColor colorWithRed:0 green:0.7 blue:0.8 alpha:1];
  749. [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
  750. [UIColor whiteColor], NSForegroundColorAttributeName,
  751. shadow,
  752. NSShadowAttributeName,
  753. [UIFont fontWithName:fontName size:fontSize], NSFontAttributeName,
  754. nil]];
  755. self.title = GBLocalizedString(@"FunVideoCrop");
  756. }
  757. - (void)createNavigationItem
  758. {
  759. NSString *fontName = GBLocalizedString(@"FontName");
  760. CGFloat fontSize = 18;
  761. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:GBLocalizedString(@"Next") style:UIBarButtonItemStylePlain target:self action:@selector(pickMusicFromCustom)];
  762. [rightItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:fontName size:fontSize]} forState:UIControlStateNormal];
  763. self.navigationItem.rightBarButtonItem = rightItem;
  764. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:GBLocalizedString(@"Settings") style:UIBarButtonItemStylePlain target:self action:@selector(showSettingsModal:)];
  765. [leftItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:fontName size:fontSize]} forState:UIControlStateNormal];
  766. self.navigationItem.leftBarButtonItem = leftItem;
  767. }
  768. - (void)createPopTipView
  769. {
  770. NSArray *colorSchemes = [NSArray arrayWithObjects:
  771. [NSArray arrayWithObjects:[NSNull null], [NSNull null], nil],
  772. [NSArray arrayWithObjects:[UIColor colorWithRed:134.0/255.0 green:74.0/255.0 blue:110.0/255.0 alpha:1.0], [NSNull null], nil],
  773. [NSArray arrayWithObjects:[UIColor darkGrayColor], [NSNull null], nil],
  774. [NSArray arrayWithObjects:[UIColor lightGrayColor], [UIColor darkTextColor], nil],
  775. nil];
  776. NSArray *colorScheme = [colorSchemes objectAtIndex:foo4random()*[colorSchemes count]];
  777. UIColor *backgroundColor = [colorScheme objectAtIndex:0];
  778. UIColor *textColor = [colorScheme objectAtIndex:1];
  779. NSString *hint = GBLocalizedString(@"UsageHint");
  780. _popTipView = [[CMPopTipView alloc] initWithMessage:hint];
  781. if (backgroundColor && ![backgroundColor isEqual:[NSNull null]])
  782. {
  783. _popTipView.backgroundColor = backgroundColor;
  784. }
  785. if (textColor && ![textColor isEqual:[NSNull null]])
  786. {
  787. _popTipView.textColor = textColor;
  788. }
  789. _popTipView.animation = arc4random() % 2;
  790. _popTipView.has3DStyle = NO;
  791. _popTipView.dismissTapAnywhere = YES;
  792. [_popTipView autoDismissAnimated:YES atTimeInterval:5.0];
  793. [_popTipView presentPointingAtView:_playButton inView:_parentView animated:YES];
  794. }
  795. - (void)viewDidLoad
  796. {
  797. [super viewDidLoad];
  798. self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"sharebg3"]];
  799. _videoPickURL = nil;
  800. [self createNavigationItem];
  801. [self createVideoView];
  802. [self createRecommendAppView];
  803. // Hint
  804. NSInteger appRunCount = [self getAppRunCount], maxRunCount = 6;
  805. if (appRunCount < maxRunCount)
  806. {
  807. [self createPopTipView];
  808. }
  809. // First run
  810. if (appRunCount == 0)
  811. {
  812. [self setShouldDisplayInnerBorder:YES];
  813. [self setShouldDisplayTextEffects:YES];
  814. [self setShouldDisplayPloygon:YES];
  815. [self setFilterType:kDistortion];
  816. // [self setShouldDisplayGhost:YES];
  817. }
  818. [self addAppRunCount];
  819. [self showVideoPlayView:NO];
  820. // Delete temp files
  821. [self deleteTempDirectory];
  822. }
  823. - (void)viewDidAppear:(BOOL)animated
  824. {
  825. [super viewDidAppear:animated];
  826. [self createNavigationBar];
  827. }
  828. - (void)didReceiveMemoryWarning
  829. {
  830. [super didReceiveMemoryWarning];
  831. // Dispose of any resources that can be recreated.
  832. }
  833. #pragma mark - Handle Event
  834. - (void)handleDemoButton
  835. {
  836. NSString *demoVideoPath = getFilePath(DemoVideoName);
  837. [self showDemoVideo:demoVideoPath];
  838. }
  839. - (void)handleConvert:(ThemesType)curThemeType
  840. {
  841. if (![self getNextStepRunCondition])
  842. {
  843. NSString *message = nil;
  844. message = GBLocalizedString(@"VideoIsEmptyHint");
  845. showAlertMessage(message, nil);
  846. return;
  847. }
  848. ProgressBarShowLoading(GBLocalizedString(@"Processing"));
  849. [[ExportEffects sharedInstance] setThemeCurrentType:curThemeType];
  850. [[ExportEffects sharedInstance] setExportProgressBlock: ^(NSNumber *percentage, NSString *title) {
  851. // Export progress
  852. NSString *content = GBLocalizedString(@"SavingVideo");
  853. if (!isStringEmpty(title))
  854. {
  855. content = title;
  856. }
  857. [self retrievingProgress:percentage title:content];
  858. }];
  859. [[ExportEffects sharedInstance] setFinishVideoBlock: ^(BOOL success, id result) {
  860. dispatch_async(dispatch_get_main_queue(), ^{
  861. if (success)
  862. {
  863. ProgressBarDismissLoading(GBLocalizedString(@"Success"));
  864. }
  865. else
  866. {
  867. ProgressBarDismissLoading(GBLocalizedString(@"Failed"));
  868. }
  869. // Alert
  870. NSString *ok = GBLocalizedString(@"OK");
  871. [UIAlertView showWithTitle:nil
  872. message:result
  873. cancelButtonTitle:ok
  874. otherButtonTitles:nil
  875. tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
  876. if (buttonIndex == [alertView cancelButtonIndex])
  877. {
  878. NSLog(@"Alert Cancelled");
  879. [NSThread sleepForTimeInterval:0.5];
  880. // Demo result video
  881. if (!isStringEmpty([ExportEffects sharedInstance].filenameBlock()))
  882. {
  883. NSString *outputPath = [ExportEffects sharedInstance].filenameBlock();
  884. [self showDemoVideo:outputPath];
  885. }
  886. }
  887. }];
  888. [self showVideoPlayView:TRUE];
  889. });
  890. }];
  891. [[ExportEffects sharedInstance] addEffectToVideo:[_videoPickURL relativePath] withAudioFilePath:getFilePath(_audioPickFile)];
  892. }
  893. - (void)handleCloseVideo:(UIView *)anchor
  894. {
  895. [self showVideoPlayView:NO];
  896. [_videoPlayerController clearView];
  897. _videoPickURL = nil;
  898. }
  899. #pragma mark - showDemoVideo
  900. - (void)showDemoVideo:(NSString *)videoPath
  901. {
  902. CGFloat statusBarHeight = iOS7AddStatusHeight;
  903. CGFloat navHeight = CGRectGetHeight(self.navigationController.navigationBar.bounds);
  904. CGSize size = [self reCalcVideoViewSize:videoPath];
  905. _demoVideoContentView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame) - size.width/2, CGRectGetMidY(self.view.frame) - size.height/2 - navHeight - statusBarHeight, size.width, size.height)];
  906. [self.view addSubview:_demoVideoContentView];
  907. // Video player of destination
  908. _demoVideoPlayerController = [[PBJVideoPlayerController alloc] init];
  909. _demoVideoPlayerController.view.frame = _demoVideoContentView.bounds;
  910. _demoVideoPlayerController.view.clipsToBounds = YES;
  911. _demoVideoPlayerController.videoView.videoFillMode = AVLayerVideoGravityResizeAspect;
  912. _demoVideoPlayerController.delegate = self;
  913. // _demoVideoPlayerController.playbackLoops = YES;
  914. [_demoVideoContentView addSubview:_demoVideoPlayerController.view];
  915. _demoPlayButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"play_button"]];
  916. _demoPlayButton.center = _demoVideoPlayerController.view.center;
  917. [_demoVideoPlayerController.view addSubview:_demoPlayButton];
  918. // Popup modal view
  919. [[KGModal sharedInstance] setCloseButtonType:KGModalCloseButtonTypeLeft];
  920. [[KGModal sharedInstance] showWithContentView:_demoVideoContentView andAnimated:YES];
  921. [self playDemoVideo:videoPath withinVideoPlayerController:_demoVideoPlayerController];
  922. }
  923. #pragma mark - NSUserDefaults
  924. #pragma mark - AppRunCount
  925. - (void)addAppRunCount
  926. {
  927. NSUInteger appRunCount = [self getAppRunCount];
  928. NSInteger limitCount = 6;
  929. if (appRunCount < limitCount)
  930. {
  931. ++appRunCount;
  932. NSString *flag = @"AppRunCount";
  933. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  934. [userDefaultes setInteger:appRunCount forKey:flag];
  935. [userDefaultes synchronize];
  936. }
  937. }
  938. - (NSUInteger)getAppRunCount
  939. {
  940. NSUInteger appRunCount = 0;
  941. NSString *flag = @"AppRunCount";
  942. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  943. if ([userDefaultes integerForKey:flag])
  944. {
  945. appRunCount = [userDefaultes integerForKey:flag];
  946. }
  947. NSLog(@"getAppRunCount: %lu", (unsigned long)appRunCount);
  948. return appRunCount;
  949. }
  950. #pragma mark - ShouldDisplayInnerBorder
  951. - (void)setShouldDisplayInnerBorder:(BOOL)shouldDisplay
  952. {
  953. NSString *flag = @"ShouldDisplayInnerBorder";
  954. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  955. if (shouldDisplay)
  956. {
  957. [userDefaultes setBool:YES forKey:flag];
  958. }
  959. else
  960. {
  961. [userDefaultes setBool:NO forKey:flag];
  962. }
  963. [userDefaultes synchronize];
  964. }
  965. #pragma mark - ShouldDisplayTextEffects
  966. - (void)setShouldDisplayTextEffects:(BOOL)shouldDisplay
  967. {
  968. NSString *flag = @"ShouldDisplayTextEffects";
  969. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  970. if (shouldDisplay)
  971. {
  972. [userDefaultes setBool:YES forKey:flag];
  973. }
  974. else
  975. {
  976. [userDefaultes setBool:NO forKey:flag];
  977. }
  978. [userDefaultes synchronize];
  979. }
  980. #pragma mark - ShouldDisplayPloygon
  981. - (void)setShouldDisplayPloygon:(BOOL)shouldDisplay
  982. {
  983. NSString *flag = @"ShouldDisplayPloygon";
  984. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  985. if (shouldDisplay)
  986. {
  987. [userDefaultes setBool:YES forKey:flag];
  988. }
  989. else
  990. {
  991. [userDefaultes setBool:NO forKey:flag];
  992. }
  993. [userDefaultes synchronize];
  994. }
  995. #pragma mark - ShouldDisplayGhost
  996. - (void)setShouldDisplayGhost:(BOOL)shouldDisplay
  997. {
  998. NSString *flag = @"ShouldDisplayGhost";
  999. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  1000. if (shouldDisplay)
  1001. {
  1002. [userDefaultes setBool:YES forKey:flag];
  1003. }
  1004. else
  1005. {
  1006. [userDefaultes setBool:NO forKey:flag];
  1007. }
  1008. [userDefaultes synchronize];
  1009. }
  1010. #pragma mark - FilterType
  1011. - (void)setFilterType:(FilterType)filterType
  1012. {
  1013. NSString *flag = @"FilterType";
  1014. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  1015. // [userDefaultes setInteger:filterType forKey:flag];
  1016. // [userDefaultes synchronize];
  1017. // Set default value
  1018. NSDictionary *userDefaultsDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
  1019. [NSNumber numberWithInteger:filterType], flag,
  1020. nil];
  1021. [userDefaultes registerDefaults:userDefaultsDefaults];
  1022. }
  1023. - (FilterType)getFilterType
  1024. {
  1025. NSString *flag = @"FilterType";
  1026. NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
  1027. if ([userDefaultes objectForKey:flag])
  1028. {
  1029. return [[userDefaultes objectForKey:flag] integerValue];
  1030. }
  1031. else
  1032. {
  1033. return kDistortion;
  1034. }
  1035. }
  1036. @end