如何加载并在斯威夫特像素级适用于iOS编辑的位图文件? [英] How do I load and edit a bitmap file at the pixel level in Swift for iOS?
本文介绍了如何加载并在斯威夫特像素级适用于iOS编辑的位图文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在斯威夫特像素级别操纵图像。这个问题被回答为目标C:<一href=\"http://stackoverflow.com/questions/22897433/how-do-i-access-and-manipulate-jpeg-image-pixels\">How我访问和处理JPEG图像的像素?,但我想看到雨燕的等效源。
I want to manipulate an image at the pixel level in Swift. This question was answered for objective c: How do I access and manipulate JPEG image pixels?, but I would like to see the equivalent source for Swift.
推荐答案
这是我如何在触摸位置获得从图像中的颜色。
This is how I am getting a color from an image at a touch location.
我翻译了这样的回答: http://stackoverflow.com/a/12579413/359578
I translated this answer: http://stackoverflow.com/a/12579413/359578
(此示例没有错误检查为无)
(This sample does no error checking for nil)
func createARGBBitmapContext(inImage: CGImage) -> CGContext {
var bitmapByteCount = 0
var bitmapBytesPerRow = 0
//Get image width, height
let pixelsWide = CGImageGetWidth(inImage)
let pixelsHigh = CGImageGetHeight(inImage)
// Declare the number of bytes per row. Each pixel in the bitmap in this
// example is represented by 4 bytes; 8 bits each of red, green, blue, and
// alpha.
bitmapBytesPerRow = Int(pixelsWide) * 4
bitmapByteCount = bitmapBytesPerRow * Int(pixelsHigh)
// Use the generic RGB color space.
let colorSpace = CGColorSpaceCreateDeviceRGB()
// Allocate memory for image data. This is the destination in memory
// where any drawing to the bitmap context will be rendered.
let bitmapData = malloc(bitmapByteCount)
// Create the bitmap context. We want pre-multiplied ARGB, 8-bits
// per component. Regardless of what the source image format is
// (CMYK, Grayscale, and so on) it will be converted over to the format
// specified here by CGBitmapContextCreate.
let context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue)
// Make sure and release colorspace before returning
return context!
}
func getPixelColorAtLocation(point:CGPoint, inImage:CGImageRef) -> NSColor {
// Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
let context = self.createARGBBitmapContext(inImage)
let pixelsWide = CGImageGetWidth(inImage)
let pixelsHigh = CGImageGetHeight(inImage)
let rect = CGRect(x:0, y:0, width:Int(pixelsWide), height:Int(pixelsHigh))
//Clear the context
CGContextClearRect(context, rect)
// Draw the image to the bitmap context. Once we draw, the memory
// allocated for the context for rendering will then contain the
// raw image data in the specified color space.
CGContextDrawImage(context, rect, inImage)
// Now we can get a pointer to the image data associated with the bitmap
// context.
let data = CGBitmapContextGetData(context)
let dataType = UnsafePointer<UInt8>(data)
let offset = 4*((Int(pixelsWide) * Int(point.y)) + Int(point.x))
let alpha = dataType[offset]
let red = dataType[offset+1]
let green = dataType[offset+2]
let blue = dataType[offset+3]
let color = NSColor(red: CGFloat(red)/255.0, green: CGFloat(green)/255.0, blue: CGFloat(blue)/255.0, alpha: CGFloat(alpha)/255.0)
// Free image data memory for the context
free(data)
return color;
}
这篇关于如何加载并在斯威夫特像素级适用于iOS编辑的位图文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文