zoukankan      html  css  js  c++  java
  • iOS8中添加的extensions总结(三)——图片编辑扩展

    • 图片编辑扩展

    注:此教程来源于http://www.raywenderlich.com的《iOS8 by Tutorials》


    1.准备

    与(二)类似的使用Imgur作为图片来源
     

    2.正文

    添加Photo Editing Extension
    之后在新生成的文件夹下找到MainInterface.storyboard为PhotoEditingViewController加上Size Classes,在这里我删掉原来的vc,加入新的vc,并设置它为启动界面,具体设置见我提供的源码中的storyboard文件,这里只具体写写它的实现类PhotoEditingViewController的使用方法。
    还要注意的一点是:由于在新工程中要使用RWTImageFilterService.h,所以要在下面添加它的实现.m文件,否则会报错!!!
    下面是PhotoEditingViewController.m代码,附有我的一些注释说明
     
      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时,系统默认是只为图片添加扩展效果,那么视频呢

    你可以在PHSupportedMediaTypes中修改你想支持的媒体类型

     
    源码点击 包括未添加扩展的original版本和修改后版本
  • 相关阅读:
    类例程_java战斗程序
    "类"的讲稿
    象棋中“车”的攻击范围_C#
    面向对象_方法_例题
    入门例子
    MyBatis
    MyBatis
    MyBatis
    Spring
    Hibernate学习
  • 原文地址:https://www.cnblogs.com/jackma86/p/5018512.html
Copyright © 2011-2022 走看看