iOS 8之后业舍,APPLE公司提供IOS應(yīng)用擴(kuò)展點(diǎn)Extension points(最近更新的ios10 又增加了iMessage功能擴(kuò)展)眷蜓。這些提供給操作系統(tǒng)的前所未有的訪問肛宋,比如照片編輯的擴(kuò)展,允許開發(fā)人員可以構(gòu)建功能到系統(tǒng)相冊應(yīng)用。
比如實(shí)現(xiàn)以下照片編輯效果
或者自己做個(gè)貼圖的編輯應(yīng)用
1荆秦、創(chuàng)建Photo Editing Extension Target
創(chuàng)建一個(gè)新的target徐钠,選擇 Photo Editing Extension
接著需要在info.plist添加配置信息
NSExtensionPointIdentifier
:照片編輯與擴(kuò)展PHSupportedMediaTypes
:設(shè)置支持編輯的類型是image類型NSExtensionMainStroyboard
:storyboard名
創(chuàng)建之后你會發(fā)現(xiàn)新target下多了PhotoEditingViewController和MainInterface.storyboard這兩個(gè)文件癌刽,這兩個(gè)是編輯圖片時(shí)候的視圖控制器,我們可以在storyboard上面創(chuàng)建UI
這里我添加collectionView于底部,實(shí)現(xiàn)如下卡片式選擇的效果
2显拜、實(shí)現(xiàn)圖片加載到控制器中
我們通過圖庫中【選擇圖片->進(jìn)入編輯模式->選擇編輯程序】 來進(jìn)入我們的應(yīng)用衡奥,控制器開啟的時(shí)候,會進(jìn)入到PHContentEditingController的以下代理方法里面
- (void)startContentEditingWithInput:(PHContentEditingInput *)contentEditingInput placeholderImage:(UIImage *)placeholderImage {
//在這里獲取選擇圖片远荠,顯示到我們的控制器中
self.input = contentEditingInput;
CGFloat imageWidth = placeholderImage.size.width;
CGFloat imageHeight = placeholderImage.size.height;
_proportion = imageWidth/imageHeight;
self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width/_proportion)];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.userInteractionEnabled = YES;
self.imageView.image = placeholderImage;
[self.imageView addGestureRecognizer:self.tapGesture];
self.imageView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
[self.view insertSubview:self.imageView belowSubview:self.toolView];
_inputImage = placeholderImage;
}
3矮固、處理圖片
拿到將要編輯的圖片之后,就要對圖片進(jìn)行修改了譬淳,我這里是給圖片添加一個(gè)貼圖,這里我就省略了collectionView的一些代理方法了档址,詳情請移步Demo
#pragma mark - 繪制貼圖到圖片中
-(UIImage *)addImageLogo:(UIImage *)img text:(UIImage *)logo
{
CGFloat selectImageW = 60.0f;
CGFloat orginImageW = self.imageView.frame.size.width;
CGFloat persen = selectImageW/orginImageW;
//get image width and height
int w = img.size.width;
int h = img.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//create a graphic context with CGBitmapContextCreate
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 44 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextDrawImage(context, CGRectMake(_iconPoint.x , _iconPoint.y-w*persen, w*persen, w*persen), [logo CGImage]);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
4、完成編輯邻梆、回調(diào)圖片
當(dāng)結(jié)束編輯的時(shí)候會來到這個(gè)代理方法里面(通過我們對圖片的一系列處理之后守伸,生成JPEG的 output 圖片,回調(diào)給
completionHandler
)
- (void)finishContentEditingWithCompletionHandler:(void (^)(PHContentEditingOutput *))completionHandler {
// Update UI to reflect that editing has finished and output is being rendered.
// Render and provide output on a background queue.
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
// Create editing output from the editing input.
PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input];
NSData *imageData = UIImageJPEGRepresentation([self addImageLogo:self.imageView.image text:_selectImage],1.0);
PHAdjustmentData *adjustmentData = [[PHAdjustmentData alloc]initWithFormatIdentifier:formatIdentifier formatVersion:formatVersion data:imageData];
// Provide new adjustments and render output to given location.
output.adjustmentData = adjustmentData;
NSData *renderedJPEGData = imageData;
[renderedJPEGData writeToURL:output.renderedContentURL atomically:YES];
// Call completion handler to commit edit to Photos.
NSLog(@">>>>>>finishContent");
completionHandler(output);
// Clean up temporary files, etc.
});
}
5浦妄、運(yùn)行
選擇Photo editing extension的target尼摹,然后command+R,選擇運(yùn)行到圖庫上
接著剂娄。蠢涝。。阅懦。
(ps)如果找不到擴(kuò)展應(yīng)用惠赫,在more里面選中即可
6、內(nèi)存限制
擴(kuò)展并不是完整的iOS應(yīng)用程序故黑,因此允許有限制地訪問系統(tǒng)資源儿咱。更具體地說,如果它使用了太多的內(nèi)存场晶,操作系統(tǒng)將殺死一個(gè)擴(kuò)展混埠。所以,如果沒有硬性限制诗轻,我建議盡量減少內(nèi)存占用钳宪。
有以下建議可以做你的圖片編輯的擴(kuò)展的內(nèi)存使用量保持在最低水平:
1、使用顯示圖像的尺寸:當(dāng)開始編輯處理中扳炬,系統(tǒng)提供適當(dāng)?shù)乜s放的屏幕圖像吏颖。使用代替了原來的交互式編輯階段,這將節(jié)省顯示圖片的內(nèi)存恨樟。
** 2半醉、限制Core Graphics上下文的數(shù)量:**如果需要使用上下文,則該數(shù)目應(yīng)該減少到最低限度劝术。
3缩多、使用GPU:可通過Core Image或第三方框架GPUImage呆奕,減少內(nèi)存占用。
小結(jié):這個(gè)開發(fā)過程其實(shí)不復(fù)雜衬吆,而注意的一點(diǎn)就是內(nèi)存的處理梁钾,盡量不要使用太占內(nèi)存的操作。大家可以嘗試一下其他的應(yīng)用擴(kuò)展逊抡,例如IOS10的IMessage 開發(fā)也是前所未有的嘗試姆泻。