最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實現(xiàn)裁剪框和圖片剪裁功能

 更新時間:2016年03月22日 15:00:12   作者:jiangamh  
這篇文章主要為大家詳細介紹了iOS實現(xiàn)裁剪框和圖片剪裁功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

圖片處理中經(jīng)常用的圖片剪裁,就是通過剪裁框確定圖片剪裁的區(qū)域,然后剪去該區(qū)域的圖片,今天實現(xiàn)了一下,其實圖片剪裁本身不難,主要剪裁框封裝發(fā)了點時間,主要功能可以拖動四個角縮放,但不能超出父視圖,拖動四個邊單方向縮放,不能超出父視圖,拖動中間部分單單移動,不改變大小,不能超出父視圖。下面列舉一些主要代碼。

四個角的處理代碼:

-(void)btnPanGesture:(UIPanGestureRecognizer*)panGesture
{
 UIView *vw = panGesture.view;
 CGRect oldFrame = self.frame;
 CGRect oldIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);

 CGPoint transport = [panGesture translationInView:vw];
 if (vw.tag == 4) {
 self.width = self.preFrame.size.width + transport.x;
 self.height = self.preFrame.size.height + transport.y;
 }
 else if(vw.tag == 3)
 {
 self.x = self.preFrame.origin.x + transport.x;
 self.width = self.preFrame.size.width - transport.x;
 self.height = self.preFrame.size.height + transport.y;
 }
 else if(vw.tag == 2)
 {
 self.width = self.preFrame.size.width + transport.x;
 self.y = self.preFrame.origin.y + transport.y;
 self.height = self.preFrame.size.height - transport.y;
 }
 else if(vw.tag == 1)
 {
 self.x = self.preFrame.origin.x + transport.x;
 self.width = self.preFrame.size.width - transport.x;
 self.y = self.preFrame.origin.y + transport.y;
 self.height = self.preFrame.size.height - transport.y;
 }
 if (panGesture.state == UIGestureRecognizerStateEnded) {
 self.preFrame = self.frame;
 }
 if (self.width < MinWidth || self.height < MinHeight) {
 self.frame = oldFrame;
 }
 CGRect newFrame = self.frame;
 if (newFrame.size.width * newFrame.size.height > oldFrame.size.height * oldFrame.size.width) {

 CGRect newIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 if (newFrame.size.width * newFrame.size.height > newIntersectRect.size.width * newIntersectRect.size.height) {
 self.frame = oldFrame;
 }
 }
 self.preCenter = self.center;
}

我是通過視圖于父視圖的frame的交集部分的面積判斷是否超出父視圖的。

四個邊的控制代碼:

-(void)viewPanGesture:(UIPanGestureRecognizer*)panGesture
{
 UIView *vw = panGesture.view;
 CGRect oldFrame = self.frame;
 CGRect oldIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);

 CGPoint transport = [panGesture translationInView:vw];
 if (vw.tag == 1) {
 self.y = self.preFrame.origin.y + transport.y;
 self.height = self.preFrame.size.height - transport.y;
 }
 else if(vw.tag == 2)
 {
 self.x = self.preFrame.origin.x + transport.x;
 self.width = self.preFrame.size.width - transport.x;
 }
 else if(vw.tag == 3)
 {
 self.height = self.preFrame.size.height + transport.y;
 }
 else if(vw.tag == 4)
 {
 self.width = self.preFrame.size.width + transport.x;
 }
 if (panGesture.state == UIGestureRecognizerStateEnded) {
 self.preFrame = self.frame;
 }
 if (self.width < MinWidth || self.height < MinHeight) {
 self.frame = oldFrame;

 }
 self.preCenter = self.center;
 CGRect newFrame = self.frame;
 if (newFrame.size.width * newFrame.size.height > oldFrame.size.height * oldFrame.size.width) {

 CGRect newIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 if (oldIntersectRect.size.width * oldIntersectRect.size.height >= newIntersectRect.size.width * newIntersectRect.size.height) {
 self.frame = oldFrame;
 self.preCenter = self.preCenter;
 }

 }

}

中間部分移動的控制代碼:

-(void)contentViewPanGestureAction:(UIPanGestureRecognizer*)panGesture
{
 CGPoint transport = [panGesture translationInView:self];
 CGRect oldFrame = self.frame;
 CGRect oldIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 CGFloat oldMj = oldIntersectRect.size.width * oldIntersectRect.size.height;

 self.center = CGPointMake(self.preCenter.x + transport.x, self.preCenter.y + transport.y);

 if (panGesture.state == UIGestureRecognizerStateEnded) {

 self.preCenter = self.center;
 }
 CGRect newIntersectRect = CGRectIntersection(self.frame, self.superview.bounds);
 CGFloat newMj = newIntersectRect.size.width * newIntersectRect.size.height;

 if (newMj < oldMj) {
 self.frame = oldFrame;
 self.preCenter = self.center;
 }
}

剪裁框?qū)崿F(xiàn)的核心代碼如上,個人覺得最不好處理的是對超出父視圖的控制,要保證不能超出父視圖,個人主要用到的是通過子視圖與父視圖的交集部分的面積的改變來獲知是否超出父視圖,如果超出父視圖,就會退到之前的frame,不知道是否還有其他好的方法,有的話可以一起交流一下。

圖片剪裁部分

代碼如下:

-(void)cropImg
{
 CGRect cropFrame = self.cropView.frame;
 CGFloat orgX = cropFrame.origin.x * (self.img.size.width / self.imgView.frame.size.width);
 CGFloat orgY = cropFrame.origin.y * (self.img.size.height / self.imgView.frame.size.height);
 CGFloat width = cropFrame.size.width * (self.img.size.width / self.imgView.frame.size.width);
 CGFloat height = cropFrame.size.height * (self.img.size.height / self.imgView.frame.size.height);
 CGRect cropRect = CGRectMake(orgX, orgY, width, height);
 CGImageRef imgRef = CGImageCreateWithImageInRect(self.img.CGImage, cropRect);

 CGFloat deviceScale = [UIScreen mainScreen].scale;
 UIGraphicsBeginImageContextWithOptions(cropFrame.size, 0, deviceScale);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextTranslateCTM(context, 0, cropFrame.size.height);
 CGContextScaleCTM(context, 1, -1);
 CGContextDrawImage(context, CGRectMake(0, 0, cropFrame.size.width, cropFrame.size.height), imgRef);
 UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
 CGImageRelease(imgRef);
 UIGraphicsEndImageContext();

 ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
 [library toolWriteImageToSavedPhotosAlbum:newImg.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
 if(error)
 {
 JGLog(@"寫入出錯");
 }
 } groupName:@"相冊名稱"];
}

這里要注意一點CGContextDrawImage這個函數(shù)的坐標系和UIKIt的坐標系上下顛倒,需對坐標系處理如下:

CGContextTranslateCTM(context, 0, cropFrame.size.height);
CGContextScaleCTM(context, 1, -1);

看看效果:

剪裁之后的圖片:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。

相關文章

  • iOS仿抖音視頻加載動畫效果的實現(xiàn)方法

    iOS仿抖音視頻加載動畫效果的實現(xiàn)方法

    這篇文章主要給大家介紹了關于iOS視頻加載動畫效果的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,下面隨著小編來一起學習學習吧
    2018-11-11
  • 老生常談iOS應用程序生命周期

    老生常談iOS應用程序生命周期

    下面小編就為大家?guī)硪黄仙U刬OS應用程序生命周期。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • IOS 開發(fā)之UITableView 刪除表格單元寫法

    IOS 開發(fā)之UITableView 刪除表格單元寫法

    這篇文章主要介紹了IOS 開發(fā)之UITableView 刪除表格單元寫法的相關資料,這里提供實例幫助大家實現(xiàn)該功能,希望能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • iOS中指紋識別常見問題匯總

    iOS中指紋識別常見問題匯總

    最近在公司做了一個app要使用指紋支付的功能,在實現(xiàn)過程中遇到各種坑,今天小編抽抗給大家總結(jié)把遇到問題匯總特此分享到腳本之家平臺,需要的朋友參考下
    2016-12-12
  • iOS高仿微信相冊界面翻轉(zhuǎn)過渡動畫效果

    iOS高仿微信相冊界面翻轉(zhuǎn)過渡動畫效果

    在圖片界面點擊右下角的查看評論會翻轉(zhuǎn)到評論界面,評論界面點擊左上角的返回按鈕會反方向翻轉(zhuǎn)回圖片界面,真正的實現(xiàn)方法,與傳統(tǒng)的導航欄過渡其實只有一行代碼的區(qū)別,下面小編通過本文給大家介紹下ios高仿微信相冊界面翻轉(zhuǎn)過渡動畫效果,一起看看吧
    2016-11-11
  • iOS My97DatePicker日歷使用詳解

    iOS My97DatePicker日歷使用詳解

    這篇文章主要為大家詳細介紹了iOS My97DatePicker日歷的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 一篇文章搞定iOS的Cookie存取

    一篇文章搞定iOS的Cookie存取

    Cookie中文名稱叫做“小型文本文件”,指某些網(wǎng)站為了辨別用戶身份而存儲在用戶本地終端上的數(shù)據(jù)(通常經(jīng)過加密),下面這篇文章主要給大家介紹了關于iOS的Cookie存取的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • iOS利用CoreImage實現(xiàn)人臉識別詳解

    iOS利用CoreImage實現(xiàn)人臉識別詳解

    OS的人臉識別從iOS 5(2011)就有了,不過一直沒怎么被關注過。人臉識別API允許開發(fā)者不僅可以檢測人臉,也可以檢測到面部的一些特殊屬性,比如說微笑或眨眼。下面這篇文章主要給大家介紹了iOS利用CoreImage實現(xiàn)人臉識別的相關資料,需要的朋友可以參考下。
    2017-05-05
  • IOS 常見內(nèi)存泄漏以及解決方案

    IOS 常見內(nèi)存泄漏以及解決方案

    這篇文章主要介紹了IOS 常見內(nèi)存泄漏以及解決方案的相關資料,需要的朋友可以參考下
    2017-05-05
  • Objective-C中NSArray的基本用法示例

    Objective-C中NSArray的基本用法示例

    這篇文章主要介紹了Objective-C中NSArray的基本用法示例,包括基本的排序等方法的介紹,需要的朋友可以參考下
    2015-09-09

最新評論

疏附县| 南华县| 兴城市| 柘荣县| 西林县| 缙云县| 济阳县| 苏尼特右旗| 苗栗市| 远安县| 拜城县| 蕉岭县| 务川| 务川| 大同县| 韶山市| 镇雄县| 唐河县| 安化县| 安阳县| 台州市| 苍梧县| 四会市| 富民县| 甘洛县| 乃东县| 安龙县| 铜梁县| 上饶市| 绍兴市| 西平县| 诸城市| 固阳县| 鹤山市| 饶河县| 河间市| 镇远县| 北票市| 丽江市| 抚宁县| 南阳市|