Skip to content

Commit

Permalink
added a PixelDataDestroyer to properly clean up the underlying PixelB…
Browse files Browse the repository at this point in the history
…itmap data buffer
  • Loading branch information
saniul committed Mar 16, 2015
1 parent f19f22b commit f7911f3
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions Prototope/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,22 @@ public struct Image {
public struct PixelBitmap : MutableCollectionType {
typealias Index = Int

var data: UnsafeMutablePointer<Pixel>
let width: Int
let height: Int

public let startIndex: Int = 0

public var endIndex: Int { return height * width }

private let dataDestroyer: PixelDataDestroyer

init(image: Image) {
width = Int(image.size.width)
height = Int(image.size.height)

data = UnsafeMutablePointer<Pixel>.alloc(width*height)
dataDestroyer = PixelDataDestroyer(data: data, width: width, height: height)

let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
Expand All @@ -57,14 +68,6 @@ public struct PixelBitmap : MutableCollectionType {

CGContextDrawImage(context, CGRect(origin: CGPointZero, size: image.uiImage.size), image.uiImage.CGImage)
}

var data: UnsafeMutablePointer<Pixel>
let width: Int
let height: Int

public let startIndex: Int = 0

public var endIndex: Int { return height * width }

public subscript (position: Int) -> Pixel {
get { return data[position] }
Expand Down Expand Up @@ -117,6 +120,22 @@ public struct PixelBitmap : MutableCollectionType {

return newBitmap
}

private class PixelDataDestroyer {
let pixelDataToCleanUp: UnsafeMutablePointer<Pixel>
let width: Int
let height: Int

init(data: UnsafeMutablePointer<Pixel>, width: Int, height: Int) {
self.pixelDataToCleanUp = data
self.width = width
self.height = height
}

deinit {
pixelDataToCleanUp.dealloc(width*height)
}
}
}


Expand Down

1 comment on commit f7911f3

@NachoSoto
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

Please sign in to comment.