美文网首页
iOS CIDetector 识别二维码失败解决方案

iOS CIDetector 识别二维码失败解决方案

作者: hehtao | 来源:发表于2019-10-07 21:58 被阅读0次

对于 CIDetector 如果目标二维码过大,有几率造成识别失败,需对图片进行缩小:

+ (UIImage *)imageSizeWithScreenImage:(UIImage *)image {
    CGFloat imageWidth = image.size.width;
    CGFloat imageHeight = image.size.height;
    CGFloat screenWidth = ScreenWidth;
    CGFloat screenHeight = ScreenHeight;
    
    if (imageWidth <= screenWidth && imageHeight <= screenHeight) {
        return image;
    }
    
    CGFloat max = MAX(imageWidth, imageHeight);
    CGFloat scale = max / (screenHeight * 2.0);
    
    CGSize size = CGSizeMake(imageWidth / scale, imageHeight / scale);
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

相关文章

网友评论

      本文标题:iOS CIDetector 识别二维码失败解决方案

      本文链接:https://www.haomeiwen.com/subject/xkdjuctx.html