关于CGContextRef开发中的一个疏忽

在这里记录一下自己犯的一个很二的问题:
我的本意是写一个oldprice,中间一根删除线。至于用NSMutableAttributedString的方法这里不考虑了。
之前代码:

- (void)drawRect:(CGRect)rect{

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(context, 0, rect.size.height / 2);
    CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    CGContextSetLineWidth(context, 1);

    CGContextClosePath(context);
    CGContextStrokePath(context);

}

运行之后,一直只有删除线,没有价钱,怎么看代码都没有错。
换种写法:

- (void)drawRect:(CGRect)rect{


//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextMoveToPoint(context, 0, rect.size.height / 2);
//    CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
//    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
//    CGContextSetLineWidth(context, 1);
//    
//    CGContextClosePath(context);
//    CGContextStrokePath(context);

    UIBezierPath *be = [UIBezierPath bezierPath];
            [be moveToPoint:CGPointMake(0, self.frame.size.height / 2)];
            [be addLineToPoint:CGPointMake(self.frame.size.width, self.frame.size.height / 2)];
            [[UIColor redColor] setStroke];
            [be closePath];
            [be stroke];

}

还是一样,很无语。
结果出去接个水回来一看,nnd,忘了super。

- (void)drawRect:(CGRect)rect{
    //一定不能忘记
    [super drawRect: rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(context, 0, rect.size.height / 2);
    CGContextAddLineToPoint(context, rect.size.width, rect.size.height / 2);
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    CGContextSetLineWidth(context, 1);

    CGContextClosePath(context);
    CGContextStrokePath(context);

}

写在这里,提醒自己。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS开发获取图片二维码的定位,可以使用CoreImage框架。具体步骤如下: 1. 使用CIImage加载图片: ``` CIImage *image = [CIImage imageWithCGImage:image.CGImage]; ``` 2. 创建CIDetector并设置识别类型: ``` CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy:CIDetectorAccuracyHigh}]; ``` 3. 识别二维码: ``` NSArray *features = [detector featuresInImage:image]; ``` 4. 遍历识别结果,获取二维码定位: ``` for (CIQRCodeFeature *feature in features) { NSArray *corners = feature.corners; // 获取定位四个角的坐标 // 在图像上绘制定位 UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor); CGContextSetLineWidth(context, 2.0); CGContextMoveToPoint(context, corners[0].x, corners[0].y); CGContextAddLineToPoint(context, corners[1].x, corners[1].y); CGContextAddLineToPoint(context, corners[2].x, corners[2].y); CGContextAddLineToPoint(context, corners[3].x, corners[3].y); CGContextAddLineToPoint(context, corners[0].x, corners[0].y); CGContextStrokePath(context); UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); } ``` 这样就可以在原图上绘制出二维码的定位了。需要注意的是,CIDetector只能识别二维码,如果要识别其他类型的码,需要设置不同的detector类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值