2015-12-24 114 views
1

首先,我的應用程序要求用戶從collectionview中選取一張照片或拍攝相機圖片,然後導航到裁剪圖像編輯器,以便根據裁剪區域位置將最終圖像放置爲正方形。但是,問題是關於它的源文件很少。沒有UIImagePicker,怎麼做?UIImageView裁剪廣場

我也試過拍方形圖片,沒有成功。

我實現了UIPinchGesture imageview,以便用戶可以放大或縮小,但圖像視圖中沒有裁剪方塊。我必須添加裁剪區域。

這是裁剪的UIImage功能:

func croppImageByRect() -> UIImage { 
    let ratio: CGFloat = 1 // square 
    let delta: CGFloat 
    let offSet: CGPoint 

    //make a new square size, that is the resized imaged width 
    let newSize = CGSizeMake(size.width, (size.width - size.width/8)) 

    //figure out if the picture is landscape or portrait, then 
    //calculate scale factor and offset 
    if (size.width > size.height) { 
     delta = (ratio * size.width - ratio * size.height) 
     offSet = CGPointMake(delta/2, 0) 
    } else { 
     delta = (ratio * size.height - ratio * size.width) 
     offSet = CGPointMake(0, delta/2) 
    } 

    //make the final clipping rect based on the calculated values 
    let clipRect = CGRectMake(-offSet.x, -offSet.y, (ratio * size.width) + delta, (ratio * size.height) + delta) 

    //start a new context, with scale factor 0.0 so retina displays get 
    //high quality image 
    UIGraphicsBeginImageContextWithOptions(newSize, true, 0.0) 

    UIRectClip(clipRect) 
    drawInRect(clipRect) 
    let newImage = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 

    return newImage 
} 

回答

0

好了,開始,這條線:

// make a new square size, that is the resized imaged width 
let newSize = CGSizeMake(size.width, (size.width - size.width/8)) 

不會導致見方大小因爲寬度和高度是不一樣的。