Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios CGBitmapContextCreate:不支持的参数组合。如何通过kCGImageAlphaNoneSkipFirst_Ios_Swift_Core Graphics - Fatal编程技术网

Ios CGBitmapContextCreate:不支持的参数组合。如何通过kCGImageAlphaNoneSkipFirst

Ios CGBitmapContextCreate:不支持的参数组合。如何通过kCGImageAlphaNoneSkipFirst,ios,swift,core-graphics,Ios,Swift,Core Graphics,我最初用Obj-C编写了这个应用程序(),但需要将其转换为Swift。转换后,我一直无法创建位图的上下文 错误消息: Whiteboard[2833] <Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 1500 bytes/row.

我最初用Obj-C编写了这个应用程序(),但需要将其转换为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!, UInt(size.width), UInt(size.height), 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), CGBitmapInfo.ByteOrder32Little);
我相信这个问题与
CGBitmapInfo.ByteOrder32Little
有关,但我不确定要传递什么。有没有办法将
kgimagealphanoneskipffirst
作为
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();
        }
    }

}
//
//白板
//白板
//
进口基金会
导入UIKit
类白板:UIView{
变量色调:CGFloat
var cacheBitmap:UnsafeMutablePointer?
var cacheContext:CGContextRef?
重写初始化(帧:CGRect){
self.hue=0.0;
//创建具有父视图大小的UIView
super.init(frame:frame);
//初始化位图的缓存上下文
自初始化上下文(框架);
//将视图的背景色设置为白色
self.backgroundColor=UIColor.whiteColor();
//在屏幕右下角添加保存按钮
let buttonFrame=CGRectMake(frame.size.width-50,frame.size.height-30,40,25);
let button=UIButton();
button.frame=按钮框架;
设置标题(“保存”,状态:。正常);
button.setTitleColor(UIColor.blueColor(),forState:.Normal);
addTarget(self,action:“downloadImage”,forControlEvents:.TouchUpInside);
//将按钮添加到视图中
self.addSubview(按钮);
}
必需的初始化(编码器aDecoder:NSCoder){
self.hue=0.0;
super.init(编码者:aDecoder)
}
func initContext(框架:CGRect)->Bool{
let size=frame.size;//获取UIView的大小
变量bitmapByteCount:UInt!
变量bitmapBytesPerRow:UInt!
//计算每行的字节数。每像素4个字节:红色、绿色、蓝色、alpha
bitmapBytesPerRow=UInt(size.width*4);
//位图中的总字节数
bitmapByteCount=UInt(CGFloat(bitmapBytesPerRow)*size.height);
//为图像数据分配内存。这是内存中任何
//将渲染位图上下文中的图形
self.cacheBitmap=malloc(bitmapByteCount);
//从位图创建缓存上下文
self.cacheContext=CGBitmapContextCreate(self.cacheBitmap!、UInt(size.width)、UInt(size.height)、8、bitmapBytesPerRow、CGColorSpaceCreateDeviceRGB()、CGBitmapInfo.ByteOrder32Little);
//将背景设置为白色
CGContextSetRGBFillColor(self.cacheContext,1.0,1.0,1.0,1.0);
CGContextFillRect(self.cacheContext,frame);
CGContextSaveGState(self.cacheContext);
返回true;
}
//每次拖动触摸事件时激发
覆盖功能触摸移动(触摸:NSSet,withEvent事件:UIEvent){
让touch=touch.anyObject()作为UITouch;
自吸式(触摸式);
}
//将新触摸事件绘制到缓存的位图
func drawToCache(触摸:UITouch){
self.hue+=0.005;
如果(self.hue>1.0){
self.hue=0.0;
}
//创建线条颜色的颜色对象
设color=UIColor(色调:CGFloat(self.hue),饱和度:CGFloat(0.7),亮度:CGFloat(1.0),alpha:CGFloat(1.0));
//设置行大小、类型和颜色
CGContextSetStrokeColorWithColor(self.cacheContext,color.CGColor);
CGContextSetLineCap(self.cacheContext,kCGLineCapRound);
CGContextSetLineWidth(self.cacheContext,CGFloat(15));
//获取当前和最后一个接触点
让lastPoint=触摸。previousLocationInView(self)作为CGPoint;
让newPoint=touch.locationInView(self)作为CGPoint;
//划清界限
CGContextMoveToPoint(self.cacheContext,lastPoint.x,lastPoint.y);
CGContextAddLineToPoint(self.cacheContext,newPoint.x,newPoint.y);
CGContextStrokePath(self.cacheContext);
//计算需要更新的脏像素
设dirtyPoint1=CGRectMake(lastPoint.x-10,lastPoint.y-10,20,20);
设dirtyPoint2=CGRectMake(newPoint.x-10,newPoint.y-10,20,20);
self.setNeedsDisplay();
//仅更新脏像素以提高性能
//self.setNeedsDisplayInRect(目录输入1);
//self.setNeedsDisplayInRect(目录输入2);
}
//将cachedBitmap绘制到UIView
重写func drawRect(rect:CGRect){
//获取当前图形上下文
让context=UIGraphicsGetCurrentContext();
//获取要绘制的图像
让cacheImage=CGBitmapContextCreateImage(self.cacheContext);
//将ImageContext绘制到屏幕上
CGContextDrawImage(上下文、self.bounds、cacheImage);
}
//将图像下载到相机卷中
func下载图像(){
//从CGContext中获取图像
让image=UIImage(CGImage:CGBitmapContextCreateImage(self.cacheContext));
//将图像保存到他们的相机卷中
UIImageWriteToSavedPhotosAlbum(图像,自我,“图像:didFinishSavingWitheror:contextInfo:”,无);
}
func图像(图像:UIImage,DidFinishSavingWither错误:NSError,contextInfo:UnsameutablePointer){
如果(!error.localizedDescription.isEmpty){
UIAlertView(标题:“错误”,消息:“保存照片时出错”,代理:无,取消按钮:“确定”).show();
}
}
}

在Objective-C中,您只需强制转换为其他枚举类型,如下所示:

(CGBitmapInfo)kCGImageAlphaNoneSkipFirst
CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)
在Swift中,您必须这样做:

(CGBitmapInfo)kCGImageAlphaNoneSkipFirst
CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)
欢迎来到疯狂而古怪的快速数字世界。必须使用
rawValue
从原始CGImageAlphaInfo枚举中提取数值;现在,您可以在CGBitmapInfo枚举的初始值设定项中使用该数值

编辑在iOS中要简单得多