Here is a class that blurs a view. I know, iOS8 has that new UIVisualEffectView
, but I am targeting iOS7.
I have an issue with an array not populating with CGImage! objects. The code runs until the end of func decreasinglyBlurredImages()
, after that I get this error:
0x410fc4: cmp r6, #0x0
0x410fc8: beq 0x411004 ; partial apply forwarder for Swift._ContiguousArrayBuffer.(_asCocoaArray <A>(Swift._ContiguousArrayBuffer<A>) -> () -> Swift._CocoaArray).(closure #1) with unmangled suffix "340" + 164
0x410fcc: ldr r0, [r5]
0x410fd0: mov r1, r4
0x410fd4: ldr r2, [r0, #12]
0x410fd8: mov r0, sp
0x410fdc: blx r2
0x410fe0: ldr r0, [r5]
0x410fe4: mov r1, r4
0x410fe8: ldr r2, [r0, #16]
0x410fec: mov r0, r8
0x410ff0: blx r2
0x410ff4: mov r0, r6
0x410ff8: sub sp, r7, #0x10
0x410ffc: ldr r8, [sp], #4
0x411000: pop {r4, r5, r6, r7, pc}
0x411004: trap Thread:3 EXE_BREAKPOINT ....
At the bottom is pretty much the whole code for this class. I was just trying to pre-populate the array for the sake of speed and ran into this issue. Interesting note, if I change optBlurredImagesForAnimation
to UIImage
and do this:
func decreasinglyBlurredImages()->[UIImage]
{
var images:[UIImage] = []
for var i = numberOfImagesForAnimation; i > -1; i--
{
var blurredImageForAnimation:UIImage = UIImage(CGImage: blurImage(i))
images.append(blurredImageForAnimation)
}
return images
}
everything works! However, I would eventually like to use this array of CGImages
in a CAKeyFrameAnimation
so I need the CGImages
.
Attaching code :
import UIKit
import Foundation
import QuartzCore
class AEBlurryImage: UIView {
let imageToBlur:UIImage
let blurFactor:NSNumber
let numberOfImagesForAnimation = 10
let buttonTitle:String?
var optBlurredImagesForAnimation:[CGImage!]?
init(frame: CGRect, image:UIImage, blurFactor:NSNumber)
{
imageToBlur = image
if blurFactor == 0
{
self.blurFactor = self.numberOfImagesForAnimation
}else
{
self.blurFactor = blurFactor
}
super.init(frame: frame)
}
func displayBlurredImage()
{
var blurredImage:CGImage!
//get the blurred image and add it to the layer
blurredImage = self.blurImage(nil)
self.layer.contents = blurredImage
if let blurredImagesForAnimation = optBlurredImagesForAnimation
{
return
}else{
let imageQueue:dispatch_queue_t = dispatch_queue_create("iQ", DISPATCH_QUEUE_SERIAL)
dispatch_async(imageQueue){
self.optBlurredImagesForAnimation = self.decreasinglyBlurredImages()
}
}
}
func blurImage(optionalFactor:NSNumber?)->CGImage!
{
var context:CIContext = CIContext(options:nil)
var image:CIImage = CIImage(image:imageToBlur)
var filterName:String? = "CIGaussianBlur"
var filter:CIFilter = CIFilter(name:filterName)
filter.setValue(image, forKey: kCIInputImageKey)
//if optionalFactor is nil, just use the blurFactor
if var factor = optionalFactor
{
filter.setValue(factor, forKey:kCIInputRadiusKey)
}else
{
filter.setValue(blurFactor, forKey: kCIInputRadiusKey)
}
var result:CIImage = filter.outputImage
var rect:CGRect = result.extent()
var cgImage:CGImage! = context.createCGImage(result, fromRect: rect)
var blurredImage:CGImage! = cgImage
return blurredImage
}
func decreasinglyBlurredImages()->[CGImage!]
{
var images:[CGImage!] = []
for var i = numberOfImagesForAnimation; i > -1; i--
{
var blurredImageForAnimation:CGImage! = self.blurImage(i)
images.append(blurredImageForAnimation)
}
return images
}
}
I think I figured out my issue. The problem was with the device I was using to develop on. The device is an iPhone 5 with iOS7 installed, not iOS8. This code works great in the simulator and that and what Rikkles said is what finally cracked it for me. Thanks again!