注:此教程來源於http://www.raywenderlich.com的《iOS8 by Tutorials》git
1 // 2 // PhotoEditingViewController.m 3 // JMImgure Photo 4 // 5 // Created by JackMa on 15/12/3. 6 // Copyright © 2015年 JackMa. All rights reserved. 7 // 8 9 #import "PhotoEditingViewController.h" 10 #import <Photos/Photos.h> 11 #import <PhotosUI/PhotosUI.h> 12 #import "RWTImageFilterService.h" 13 14 @interface PhotoEditingViewController () <PHContentEditingController> 15 16 @property (strong) PHContentEditingInput *input; 17 18 @property (nonatomic, weak) IBOutlet UIImageView *imageView; 19 @property (nonatomic, weak) IBOutlet UIButton *undoButton; 20 @property (nonatomic, weak) IBOutlet UIButton *addButton; 21 22 @property (strong, nonatomic) RWTImageFilterService *imageFilterService; 23 @property (strong, nonatomic) NSString *currentFilterName; 24 @property (strong, nonatomic) UIImage *filteredImage; 25 26 @end 27 28 @implementation PhotoEditingViewController 29 30 //撤銷的button 31 - (IBAction)undo:(id)sender { 32 self.imageView.image = self.input.displaySizeImage; 33 self.currentFilterName = nil; 34 self.filteredImage = nil; 35 } 36 37 //添加過濾器 38 - (IBAction)addFilter:(id)sender { 39 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"過濾器" message:@"請選擇一種過濾器" preferredStyle:UIAlertControllerStyleActionSheet]; 40 //遍歷字典 41 [self.imageFilterService.availableFilters enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { 42 UIAlertAction *action = [UIAlertAction actionWithTitle:key style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 43 //爲圖片添加過濾 44 self.filteredImage = [self.imageFilterService applyFilter:obj toImage:self.input.displaySizeImage]; 45 self.imageView.image = self.filteredImage; 46 self.currentFilterName = obj; 47 }]; 48 [alert addAction:action]; 49 }]; 50 UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 51 [alert dismissViewControllerAnimated:YES completion:nil]; 52 }]; 53 [alert addAction:cancel]; 54 [self presentViewController:alert animated:YES completion:nil]; 55 } 56 57 - (void)viewDidLoad { 58 [super viewDidLoad]; 59 60 self.imageView.contentMode = UIViewContentModeScaleAspectFit; 61 self.imageFilterService = [[RWTImageFilterService alloc] init]; 62 } 63 64 - (void)didReceiveMemoryWarning { 65 [super didReceiveMemoryWarning]; 66 // Dispose of any resources that can be recreated. 67 } 68 69 #pragma mark - PHContentEditingController 70 71 //在原始編輯界面能夠對圖片進行調整,當你在這個App想獲取的是最原始的圖片,那麼返回NO 72 //你想獲取調整後的adjustmentData的話,那麼返回YES 73 //只有圖片通過調整纔會調用此函數,不然默認NO 74 - (BOOL)canHandleAdjustmentData:(PHAdjustmentData *)adjustmentData { 75 return NO; 76 } 77 78 //view Load後,view appear前調用,用來接受原始數據contentEditingInput 79 - (void)startContentEditingWithInput:(PHContentEditingInput *)contentEditingInput placeholderImage:(UIImage *)placeholderImage { 80 //canHandleAdjustmentData:返回YES的話,這裏的contentEditingInput也會帶上adjustmentData 81 //canHandleAdjustmentData:返回NO的話,這裏只有原始圖像displaySizeImage 82 self.input = contentEditingInput; 83 self.imageView.image = self.input.displaySizeImage;//將原始圖片呈現出來 84 } 85 86 //點擊右上方完成button時調用 87 - (void)finishContentEditingWithCompletionHandler:(void (^)(PHContentEditingOutput *))completionHandler { 88 //異步處理圖片 89 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 90 //建立output並設置 91 PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input]; 92 NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:self.currentFilterName]; 93 #warning Please set your Photos Extension Name and Version here 94 PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"qq100858433.JMImgure.JMImgure-Photo" formatVersion:@"1.0" data:archiveData]; 95 output.adjustmentData = adjustmentData; 96 97 UIImage *fullImage = [UIImage imageWithContentsOfFile:self.input.fullSizeImageURL.path]; 98 fullImage = [self.imageFilterService applyFilter:self.currentFilterName toImage:fullImage]; 99 100 //將轉化後的圖片存到renderedContentURL中 101 NSData *jpegData = UIImageJPEGRepresentation(fullImage, 1.0); 102 BOOL saved = [jpegData writeToURL:output.renderedContentURL options:NSDataWritingAtomic error:nil]; 103 if (saved) { 104 //回調處理結果給Photos 105 //注:這裏模擬機調試會出現沒法顯示修改後圖片問題,真機調試沒有問題 106 completionHandler(output); 107 } else { 108 NSLog(@"An error occurred during save"); 109 completionHandler(nil); 110 } 111 // Clean up temporary files, etc. 112 }); 113 } 114 115 //點擊左上方取消後調用 116 - (BOOL)shouldShowCancelConfirmation { 117 //返回YES,則會彈出AlertSheet讓用戶確認是否取消 118 //返回NO,則頁面直接消失 119 return NO; 120 } 121 122 //shouldShowCancelConfirmation或finishContentEditingWithCompletionHandler: 123 //以後調用,此函數在後臺運行,負責清理臨時文件 124 - (void)cancelContentEditing { 125 // Clean up temporary files, etc. 126 // May be called after finishContentEditingWithCompletionHandler: while you prepare output. 127 } 128 129 @end
在新建Photo Extension Target時,系統默認是隻爲圖片添加擴展效果,那麼視頻呢github