iOS调试小助手QuickLook

作者: handyTOOL | 来源:发表于2017-07-21 14:35 被阅读332次

    我们在调试iOS的时候常常会用到下面这个小功能。



    Debug时鼠标移动到变量上,等到辅助工具栏出现,点击眼睛按钮就可以预览这个变量的内容。最方便的地方莫过于可以预览图片,甚至是View。苹果内置了几种可以快速预览的类型。


    上面有些是iOS的类型有些是Mac App中的类型,具体的效果大家可以自行尝试。本文将要介绍的是如何给自己的类添加快速预览效果。

    实现效果

    QuickLookTarget是实现了自定义快速预览的类。在Debug时快速预览效果如下。


    图中QuickLookTarget的三个属性都在快速预览中显示了出来。

    QuickLookTarget结构

    QuickLookTarget类有三个属性。两个字符串和一个图片。

    @property (copy, nonatomic) NSString *title;
    @property (copy, nonatomic) NSString *content;
    @property (strong, nonatomic) UIImage *headImage;
    

    支持快速预览需要做的事

    QuickLookTarget类中增加- (id)debugQuickLookObject方法即可支持快速预览,debugQuickLookObject方法可以返回任何苹果支持的内置快速预览类型。本文使用的是图片UIImage。通过UIKit将属性信息绘制到图片上,然后返回。

    - (id)debugQuickLookObject {
        CGSize imageSize = CGSizeMake(400, 700);
        UIGraphicsBeginImageContext(imageSize);
        uint32_t propertyCount;
        objc_property_t * propertyList = class_copyPropertyList(self.class, &propertyCount);
        CGFloat currentYLoc = 10;
        for (int i = 0; i < propertyCount; ++i) {
            objc_property_t property = *propertyList;
            NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
            NSString *propertyAttrs = [NSString stringWithUTF8String:property_getAttributes(property)];
            if ([propertyAttrs containsString: @"NSString"]) {
                NSString *propertyValue = [self valueForKey:propertyName];
                NSString *displayText = [NSString stringWithFormat:@"%@:\n %@", propertyName, propertyValue];
                CGRect textRect = [displayText boundingRectWithSize:CGSizeMake(imageSize.width, imageSize.height) options:NSStringDrawingUsesLineFragmentOrigin  attributes:nil context:nil];
                [displayText drawWithRect:CGRectMake(textRect.origin.x, currentYLoc + textRect.origin.y, textRect.size.width, textRect.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
                currentYLoc += textRect.size.height + 10;
            } else if ([propertyAttrs containsString: @"UIImage"]) {
                NSString *displayText = [NSString stringWithFormat:@"%@: ", propertyName];
                CGRect textRect = [displayText boundingRectWithSize:CGSizeMake(imageSize.width, imageSize.height) options:NSStringDrawingUsesLineFragmentOrigin  attributes:nil context:nil];
                [displayText drawWithRect:CGRectMake(textRect.origin.x, currentYLoc + textRect.origin.y, textRect.size.width, textRect.size.height) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil context:nil];
                currentYLoc += textRect.size.height + 10;
                
                UIImage *propertyValue = [self valueForKey:propertyName];
                [propertyValue drawInRect:CGRectMake(0, currentYLoc, imageSize.width, imageSize.width / propertyValue.size.width * propertyValue.size.height)];
                currentYLoc += imageSize.width / propertyValue.size.width * propertyValue.size.height;
            }
            propertyList++;
        }
        
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        return image;
    }
    

    debugQuickLookObject中使用runtime获取所有属性,然后遍历属性绘制字符串和图片类型的数据。对于快速预览的支持就到此结束了。

    相关文章

      网友评论

        本文标题:iOS调试小助手QuickLook

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