CGBitmapContextCreate:不支持的参数组合如何传递kCGImageAlphaNoneSkipFirst [重复]

如何解决CGBitmapContextCreate:不支持的参数组合如何传递kCGImageAlphaNoneSkipFirst [重复]

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

(CGBitmapInfo)kCGImageAlphaNoneSkipFirst

在Swift中,您必须这样做:

CGBitmapInfo(CGImageAlphaInfo.NoneSkipFirst.rawValue)

欢迎来到Swift数值的狂野古怪的世界。您必须使用rawValue; 将数值从原始CGImageAlphaInfo枚举中拉出。现在,您可以在CGBitmapInfo枚举的初始化程序中使用该数值。

在iOS 9 / Swift 2.0中,这要简单得多,您可以CGImageAlphaInfo.NoneSkipFirst.rawValue直接 将其传递到CGBitmapContextCreate中,该位置现在只需要一个整数即可。

解决方法

我最初是用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),CGBitmapInfo.ByteOrder32Little);

我相信这个问题与有关CGBitmapInfo.ByteOrder32Little,但是我不确定该怎么做。有没有办法通过kCGImageAlphaNoneSkipFirstCGBitmapInfo

全文:

//
//  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!,CGBitmapInfo.ByteOrder32Little);

        // Set the background as white
        CGContextSetRGBFillColor(self.cacheContext,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);

        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();
        }
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-