CGImageDestinationAddImage writes image without blue elements

419 views Asked by At

For some reason when gifArrayAdjusted: [CGImage] has 17 or more CGImages, gif is created correctly in full color.

But when gifArrayAdjusted: [CGImage] has 16 or less CGImages, gif is created but is missing all blue elements. Red, green and grey scales are present in gif but its like blue is 'invisible'.

Tested: when viewing gifArrayAdjusted in either case, the images have full color. Therefore I assume the issue is in the createGIF func.

Note: this has to do with timing or CPU resource somehow, due to my for in loop. If I just use my gifArray and skip emphasizing certain frames, full color is ALWAYS present.

Can anyone explain why this was happening?


{
var gifArray = [CGImage]()

// Fill [CGImage] with CoverPage
let rect = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
UIGraphicsBeginImageContextWithOptions(RVC.MainView.frame.size, false, 1.0)
#imageLiteral(resourceName: "CoverPage").draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
gifArray.append(newImage!.cgImage!)


// Fill [CGImage] with rest of images
repeat {
     UIGraphicsBeginImageContext(RVC.MainView.frame.size)
     RVC.MainView.layer.render(in: UIGraphicsGetCurrentContext()!)
     let image = UIGraphicsGetImageFromCurrentImageContext()!
     UIGraphicsEndImageContext()
     let cgImage = image.cgImage
     gifArray.append(cgImage!)
     RVC.ReplayForward()
} while RVC.loopSwitch == 0


// Certain images are double entered for longer showing in gif according to RVC.specialGifFrames:[Int]
var gifArrayAdjusted = [CGImage]()
for frame in 0..<RVC.specialGifFrames.count {
    if RVC.specialGifFrames[frame] == 1 {
        gifArrayAdjusted.append(gifArray[frame])
    } else if RVC.specialGifFrames[frame] == 2 {
        gifArrayAdjusted.append(gifArray[frame])
        gifArrayAdjusted.append(gifArray[frame])
    }
}


//Create GifData
let CFData1 = CFDataCreateMutable(kCFAllocatorDefault, 0)
let GifData = createGIF(with: gifArrayAdjusted, data: CFData1!, loopCount: 0, frameDelay: 1.1)


// Save Data                
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString             
let pathed = documentsPath.appendingPathComponent(path)
    do {
        try GifData.write(to: URL(fileURLWithPath: pathed), options: .atomic)
    } catch _ {
    }
}

func createGIF(with images: [CGImage], data: CFMutableData, loopCount: Int, frameDelay: Double) -> Data {
    let gifDest = CGImageDestinationCreateWithData(data, kUTTypeGIF, images.count, nil)
    let fileProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: loopCount]]
    CGImageDestinationSetProperties(gifDest!, fileProperties as CFDictionary?)
    let frameProperties = [(kCGImagePropertyGIFDictionary as String): [(kCGImagePropertyGIFDelayTime as String): frameDelay]]
    for img in images {
        CGImageDestinationAddImage(gifDest!, img, frameProperties as CFDictionary?)
    }
    CGImageDestinationFinalize(gifDest!)
    return data as Data
}
0

There are 0 answers