2016-03-02 37 views
1

我想從scrollView抓取當前縮放圖像,將其裁剪到當前幀,將其更改爲黑白,然後重新顯示裁剪後的圖像以黑色和白色滾動查看。從UIScrollView抓取當前圖像,轉換成黑白圖像,然後回滾到滾動視圖

當我在下面運行我的代碼(來自buttonAction)後,scrollView似乎沒有顯示任何東西,但我仍然可以滾動並放大不存在的圖像。沒有編譯錯誤/警告或seg故障。

任何想法,我哪裏出錯了?

func grabImageFromCurrentView() -> UIImage { 
    // grab image from current zoom 

    // Note: "CIImage(image: imageToChange)" fails later on if the "false" here is set to true 
    // it has something to do with setting the image to RGBA instead of RGB 
    UIGraphicsBeginImageContextWithOptions(scrollView.bounds.size, false, UIScreen.mainScreen().scale) 

    let offset = scrollView.contentOffset 

    CGContextTranslateCTM(UIGraphicsGetCurrentContext(), -offset.x, -offset.y) 
    scrollView.layer.renderInContext(UIGraphicsGetCurrentContext()!) 

    let image = UIGraphicsGetImageFromCurrentImageContext() 

    UIGraphicsEndImageContext() 
    return image 
} 

// turn image to black and white image 
func setNoirFilter(imageToChange: UIImage) -> UIImage { 

    let originalFrameImage = CIImage(image: imageToChange) 
    let filter = CIFilter(name: "CIPhotoEffectNoir") 
    filter?.setDefaults() 
    filter?.setValue(originalFrameImage, forKey: kCIInputImageKey) 
    let imageOutput = filter!.outputImage 


    let image = UIImage(CIImage: imageOutput!) 

    return image 
} 

func displayImage(image: UIImage) { 
    let originalImage = image 

    imageView.image = originalImage 
    imageView.contentMode = UIViewContentMode.Center 
    imageView.frame = CGRectMake(0,0, originalImage.size.width, originalImage.size.height) 

    scrollView.contentSize = originalImage.size 

    let scrollViewFrame = scrollView.frame 
    let scaleWidth = scrollViewFrame.size.width/scrollView.contentSize.width 
    let scaleHeight = scrollViewFrame.size.height/scrollView.contentSize.height 
    let minScale = min(scaleHeight, scaleWidth) 

    scrollView.minimumZoomScale = minScale 
    scrollView.maximumZoomScale = 50 // set so high for high resolution images I'll be using 
    scrollView.zoomScale = minScale 

    centerScrollViewContents() 
} 

@IBAction func buttonAction(sender: UIButton) { 

    let currentViewImage = grabImageFromCurrentView() 

    let blackAndWhiteImage = setNoirFilter(currentViewImage) 

    displayImage(blackAndWhiteImage) 
} 
+0

嘗試使用一些斷點來查看它出錯的位置。 – Koen

回答

1

我仍然不知道爲什麼我就沒有工作,但不知何故,在setNoirFilter的「形象」將成爲釋放。它沒有任何意義,我在一個新項目中使用完全相同的代碼,它工作得很好。

但是,由於this guy我能夠用我的setNoirFilter函數代替它,它工作得很好。

func convertToGrayScale(image: UIImage) -> UIImage { 
    let imageRect:CGRect = CGRectMake(0, 0, image.size.width, image.size.height) 
    let colorSpace = CGColorSpaceCreateDeviceGray() 
    let width = image.size.width 
    let height = image.size.height 

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue) 
    let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue) 

    CGContextDrawImage(context, imageRect, image.CGImage) 
    let imageRef = CGBitmapContextCreateImage(context) 
    let newImage = UIImage(CGImage: imageRef!) 

    return newImage 
}