我最初是用Obj-C编写此应用程序(GitHub),但需要将其转换为Swift。转换后,我一直难以获取创建位图的上下文。
错误信息:
Whiteboard[2833] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 1500 bytes/row.
本来我有这个:
self.cacheContext = CGBitmapContextCreate (self.cacheBitmap, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst);
现在我有:
self.cacheContext = CGBitmapContextCreate(self.cacheBitmap!, UInt(size.width), UInt(size.height), 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), CGBitmapInfo.ByteOrder32Little);
我相信这个问题与有关CGBitmapInfo.ByteOrder32Little
,但是我不确定该怎么做。有没有办法通过kCGImageAlphaNoneSkipFirst
的CGBitmapInfo
?
全文:
//
// WhiteBoard.swift
// Whiteboard
//
import Foundation
import UIKit
class WhiteBoard: UIView {
var hue: CGFloat
var cacheBitmap: UnsafeMutablePointer<Void>?
var cacheContext: CGContextRef?
override init(frame: CGRect) {
self.hue = 0.0;
// Create a UIView with the size of the parent view
super.init(frame: frame);
// Initialize the Cache Context of the bitmap
self.initContext(frame);
// Set the background color of the view to be White
self.backgroundColor = UIColor.whiteColor();
// Add a Save Button to the bottom right corner of the screen
let buttonFrame = CGRectMake(frame.size.width - 50, frame.size.height - 30, 40, 25);
let button = UIButton();
button.frame = buttonFrame;
button.setTitle("Save", forState: .Normal);
button.setTitleColor(UIColor.blueColor(), forState: .Normal);
button.addTarget(self, action: "downloadImage", forControlEvents: .TouchUpInside);
// Add the button to the view
self.addSubview(button);
}
required init(coder aDecoder: NSCoder) {
self.hue = 0.0;
super.init(coder: aDecoder)
}
func initContext(frame: CGRect)-> Bool {
let size = frame.size; // Get the size of the UIView
var bitmapByteCount: UInt!
var bitmapBytesPerRow: UInt!
// Calculate the number of bytes per row. 4 bytes per pixel: red, green, blue, alpha
bitmapBytesPerRow = UInt(size.width * 4);
// Total Bytes in the bitmap
bitmapByteCount = UInt(CGFloat(bitmapBytesPerRow) * size.height);
// Allocate memory for image data. This is the destination in memory where any
// drawing to the bitmap context will be rendered
self.cacheBitmap = malloc(bitmapByteCount);
// Create the Cache Context from the Bitmap
self.cacheContext = CGBitmapContextCreate(self.cacheBitmap!, UInt(size.width), UInt(size.height), 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), CGBitmapInfo.ByteOrder32Little);
// Set the background as white
CGContextSetRGBFillColor(self.cacheContext, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(self.cacheContext, frame);
CGContextSaveGState(self.cacheContext);
return true;
}
// Fired everytime a touch event is dragged
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as UITouch;
self.drawToCache(touch);
}
// Draw the new touch event to the cached Bitmap
func drawToCache(touch: UITouch) {
self.hue += 0.005;
if(self.hue > 1.0) {
self.hue = 0.0;
}
// Create a color object of the line color
let color = UIColor(hue: CGFloat(self.hue), saturation: CGFloat(0.7), brightness: CGFloat(1.0), alpha: CGFloat(1.0));
// Set the line size, type, and color
CGContextSetStrokeColorWithColor(self.cacheContext, color.CGColor);
CGContextSetLineCap(self.cacheContext, kCGLineCapRound);
CGContextSetLineWidth(self.cacheContext, CGFloat(15));
// Get the current and last touch point
let lastPoint = touch.previousLocationInView(self) as CGPoint;
let newPoint = touch.locationInView(self) as CGPoint;
// Draw the line
CGContextMoveToPoint(self.cacheContext, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(self.cacheContext, newPoint.x, newPoint.y);
CGContextStrokePath(self.cacheContext);
// Calculate the dirty pixels that needs to be updated
let dirtyPoint1 = CGRectMake(lastPoint.x-10, lastPoint.y-10, 20, 20);
let dirtyPoint2 = CGRectMake(newPoint.x-10, newPoint.y-10, 20, 20);
self.setNeedsDisplay();
// Only update the dirty pixels to improve performance
//self.setNeedsDisplayInRect(dirtyPoint1);
//self.setNeedsDisplayInRect(dirtyPoint2);
}
// Draw the cachedBitmap to the UIView
override func drawRect(rect: CGRect) {
// Get the current Graphics Context
let context = UIGraphicsGetCurrentContext();
// Get the Image to draw
let cacheImage = CGBitmapContextCreateImage(self.cacheContext);
// Draw the ImageContext to the screen
CGContextDrawImage(context, self.bounds, cacheImage);
}
// Download the image to the camera roll
func downloadImage() {
// Get the Image from the CGContext
let image = UIImage(CGImage: CGBitmapContextCreateImage(self.cacheContext));
// Save the Image to their Camera Roll
UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil);
}
func image(image: UIImage, didFinishSavingWithError error: NSError, contextInfo: UnsafeMutablePointer<Void>) {
if(!error.localizedDescription.isEmpty) {
UIAlertView(title: "Error", message: "Error Saving Photo", delegate: nil, cancelButtonTitle: "Ok").show();
}
}
}
在Objective-C中,您只需将其转换为其他枚举类型,如下所示:
(CGBitmapInfo)kCGImageAlphaNoneSkipFirst
在Swift中,您必须这样做:
CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)
欢迎来到Swift数值的狂野古怪的世界。您必须使用rawValue
;
将数值从原始CGImageAlphaInfo枚举中拉出。现在,您可以在CGBitmapInfo枚举的初始化html" target="_blank">程序中使用该数值。
编辑 在iOS 9 / Swift 2.0中,这要简单得多,您可以CGImageAlphaInfo.NoneSkipFirst.rawValue
直接 将其传递到CGBitmapContextCreate中,该位置现在只需要一个整数即可。
我正在尝试使用Ansible-Playbook在Ubuntu机器上安装Apache2,PHP。我在执行playbook后得到以下错误 致命:[18.220.215.181]:失败!=>{“changed”:false,“msg”:“(systemd)模块不支持的参数:启用支持的参数包括:daemon_reexec,daemon_reload,enabled,force,masked,name,no
问题内容: 对不起这个基本问题。我想将一个切片作为参数传递给。像这样: 结果将是,但这显然不起作用。 (我要格式化的字符串比这要复杂的多,因此,简单的串联是不会做到的:) 所以问题是:如果我有数组,如何将其作为单独的参数传递给?或者:我可以调用在Go中传递参数列表的函数吗? 问题答案: 正如您在IRC上发现的那样,它将起作用: 您的原始代码无法正常工作,因为接受a 并且无法将其隐式或显式转换为该类
如何传递checkbox数组 传递数组基本上很简单,在模板里面这样写: <input size="40" type="checkbox" name="usergroup" /> <input size="40" type="checkbox" name="usergroup" /> <input size="40" type="checkbox" name="usergroup" /> 此处唯一
我有三个文件(完整的项目是https://github.com/enginyilmaz/kpbduser) mapscreen.js fetchdata.js showdata.js
我有一个react组件,我想用不同的参数调用相同的箭头函数,但我被困在如何传递参数给它,现在我在问自己,我能做到吗?
我是的新用户,我想回忆不同变量的代码块,我想做如下事情: 这里的都是中的变量。如果我调用,这部分就可以工作了。但是,我调用的方式仍然没有告诉这个函数group by是由完成的。我怎么让它工作?