美文网首页YYKit学习YYKit源码探究
YYKit源码探究(二十四) —— UIPasteboard分类

YYKit源码探究(二十四) —— UIPasteboard分类

作者: 刀客传奇 | 来源:发表于2018-03-28 00:19 被阅读33次

    版本记录

    版本号 时间
    V1.0 2018.03.28

    前言

    iOS圈内有几个人大家基本都知道,比如说王巍、唐巧,还有YYKit框架的作者现任职于滴滴的郭曜源 - ibireme等。这里有一篇唐巧对他的专访,还有他的 GitHub - Yaoyuan博客,这里贴出来框架YYKit 框架。接下来几篇我们就一起来看一下这个框架。感兴趣的可以看上面写的几篇。
    1. YYKit源码探究(一) —— 基本概览
    2. YYKit源码探究(二) —— NSString分类之Hash(一)
    3. YYKit源码探究(三) —— NSString分类之Encode and decode(二)
    4. YYKit源码探究(四) —— NSString分类之Drawing(三)
    5. YYKit源码探究(五) —— NSString分类之Regular Expression(四)
    6. YYKit源码探究(六) —— NSString分类之NSNumber Compatible(五)
    7. YYKit源码探究(七) —— NSString分类之Utilities(六)
    8. YYKit源码探究(八) —— NSNumber分类(一)
    9. YYKit源码探究(九) —— UIFont分类之架构分析和Font Traits(一)
    10. YYKit源码探究(十) —— UIFont分类之Create font(二)
    11. YYKit源码探究(十一) —— UIFont分类之Load and unload font(三)
    12. YYKit源码探究(十二) —— UIFont分类之Dump font data(四)
    13. YYKit源码探究(十三) —— UIImage分类之框架结构和Create image部分(一)
    14. YYKit源码探究(十四) —— UIImage分类之Image Info(二)
    15. YYKit源码探究(十五) —— UIImage分类之Modify Image(三)
    16. YYKit源码探究(十六) —— UIImage分类之Image Effect(四)
    17. YYKit源码探究(十七) —— UIImageView分类之架构和image部分(一)
    18. YYKit源码探究(十八) —— UIImageView分类之highlight image部分(二)
    19. YYKit源码探究(十九) —— UIScreen分类(一)
    20. YYKit源码探究(二十) —— UIScrollView分类(一)
    21. YYKit源码探究(二十一) —— UITableView分类(一)
    22. YYKit源码探究(二十二) —— UITextField分类(一)
    23. YYKit源码探究(二十三) —— UIView分类(一)

    回顾

    上一篇主要介绍了UIView分类,这一篇主要看一下UIPasteboard部分。


    API

    下面我们看一下API。

    @property (nullable, nonatomic, copy) NSData *PNGData;    ///< PNG file data
    @property (nullable, nonatomic, copy) NSData *JPEGData;   ///< JPEG file data
    @property (nullable, nonatomic, copy) NSData *GIFData;    ///< GIF file data
    @property (nullable, nonatomic, copy) NSData *WEBPData;   ///< WebP file data
    @property (nullable, nonatomic, copy) NSData *imageData;  ///< image file data
    
    /// Attributed string,
    /// Set this attributed will also set the string property which is copy from the attributed string.
    /// If the attributed string contains one or more image, it will also set the `images` property.
    @property (nullable, nonatomic, copy) NSAttributedString *attributedString;
    

    下面看一下详细的API

    1. @property (nullable, nonatomic, copy) NSData *PNGData;

    该属性就是获取PNG文件数据。

    方法实现

    - (NSData *)PNGData {
        return [self dataForPasteboardType:(id)kUTTypePNG];
    }
    
    - (void)setPNGData:(NSData *)PNGData {
        [self setData:PNGData forPasteboardType:(id)kUTTypePNG];
    }
    

    2. @property (nullable, nonatomic, copy) NSData *JPEGData;

    该属性就是获取JPEG文件数据。

    方法实现

    - (NSData *)JPEGData {
        return [self dataForPasteboardType:(id)kUTTypeJPEG];
    }
    
    - (void)setJPEGData:(NSData *)JPEGData {
        [self setData:JPEGData forPasteboardType:(id)kUTTypeJPEG];
    }
    

    3. @property (nullable, nonatomic, copy) NSData *GIFData;

    该属性就是获取GIF文件数据。

    方法实现

    - (NSData *)GIFData {
        return [self dataForPasteboardType:(id)kUTTypeGIF];
    }
    
    - (void)setGIFData:(NSData *)GIFData {
        [self setData:GIFData forPasteboardType:(id)kUTTypeGIF];
    }
    

    4. @property (nullable, nonatomic, copy) NSData *WEBPData;

    该属性就是获取WEBP文件数据。

    方法实现

    - (NSData *)WEBPData {
        return [self dataForPasteboardType:YYUTTypeWEBP];
    }
    
    - (void)setWEBPData:(NSData *)WEBPData {
        [self setData:WEBPData forPasteboardType:YYUTTypeWEBP];
    }
    

    5. @property (nullable, nonatomic, copy) NSData *imageData

    该属性就是获取图像文件数据。

    方法实现

    - (NSData *)imageData {
        return [self dataForPasteboardType:(id)kUTTypeImage];
    }
    
    - (void)setImageData:(NSData *)imageData {
        [self setData:imageData forPasteboardType:(id)kUTTypeImage];
    }
    

    6. @property (nullable, nonatomic, copy) NSAttributedString *attributedString;

    设置此属性还将设置属性字符串中的字符串属性。如果属性字符串包含一个或多个图像,它也会设置images属性。

    方法实现

    - (NSAttributedString *)attributedString {
        for (NSDictionary *items in self.items) {
            NSData *data = items[YYPasteboardTypeAttributedString];
            if (data) {
                return [NSAttributedString unarchiveFromData:data];
            }
        }
        return nil;
    }
    
    NSString *const YYPasteboardTypeAttributedString = @"com.ibireme.NSAttributedString"
    
    + (instancetype)unarchiveFromData:(NSData *)data {
        NSAttributedString *one = nil;
        @try {
            one = [YYTextUnarchiver unarchiveObjectWithData:data];
        }
        @catch (NSException *exception) {
            NSLog(@"%@",exception);
        }
        return one;
    }
    
    - (void)setAttributedString:(NSAttributedString *)attributedString {
        self.string = [attributedString plainTextForRange:NSMakeRange(0, attributedString.length)];
        NSData *data = [attributedString archiveToData];
        if (data) {
            NSDictionary *item = @{YYPasteboardTypeAttributedString : data};
            [self addItems:@[item]];
        }
        [attributedString enumerateAttribute:YYTextAttachmentAttributeName inRange:NSMakeRange(0, attributedString.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(YYTextAttachment *attachment, NSRange range, BOOL *stop) {
            UIImage *img = attachment.content;
            if ([img isKindOfClass:[UIImage class]]) {
                NSDictionary *item = @{@"com.apple.uikit.image" : img};
                [self addItems:@[item]];
                
                
                if ([img isKindOfClass:[YYImage class]] && ((YYImage *)img).animatedImageData) {
                    if (((YYImage *)img).animatedImageType == YYImageTypeGIF) {
                        NSDictionary *item = @{(id)kUTTypeGIF : ((YYImage *)img).animatedImageData};
                        [self addItems:@[item]];
                    } else if (((YYImage *)img).animatedImageType == YYImageTypePNG) {
                        NSDictionary *item = @{(id)kUTTypePNG : ((YYImage *)img).animatedImageData};
                        [self addItems:@[item]];
                    } else if (((YYImage *)img).animatedImageType == YYImageTypeWebP) {
                        NSDictionary *item = @{(id)YYUTTypeWEBP : ((YYImage *)img).animatedImageData};
                        [self addItems:@[item]];
                    }
                }
                
                
                // save image
                UIImage *simpleImage = nil;
                if ([attachment.content isKindOfClass:[UIImage class]]) {
                    simpleImage = attachment.content;
                } else if ([attachment.content isKindOfClass:[UIImageView class]]) {
                    simpleImage = ((UIImageView *)attachment.content).image;
                }
                if (simpleImage) {
                    NSDictionary *item = @{@"com.apple.uikit.image" : simpleImage};
                    [self addItems:@[item]];
                }
                
                // save animated image
                if ([attachment.content isKindOfClass:[UIImageView class]]) {
                    UIImageView *imageView = attachment.content;
                    YYImage *image = (id)imageView.image;
                    if ([image isKindOfClass:[YYImage class]]) {
                        NSData *data = image.animatedImageData;
                        YYImageType type = image.animatedImageType;
                        if (data) {
                            switch (type) {
                                case YYImageTypeGIF: {
                                    NSDictionary *item = @{(id)kUTTypeGIF : data};
                                    [self addItems:@[item]];
                                } break;
                                case YYImageTypePNG: { // APNG
                                    NSDictionary *item = @{(id)kUTTypePNG : data};
                                    [self addItems:@[item]];
                                } break;
                                case YYImageTypeWebP: {
                                    NSDictionary *item = @{(id)YYUTTypeWEBP : data};
                                    [self addItems:@[item]];
                                } break;
                                default: break;
                            }
                        }
                    }
                }
                
            }
        }];
    }
    

    后记

    本篇主要讲述了UIPasteboard的分类,感兴趣的可以给个关注和赞,谢谢~~~~

    相关文章

      网友评论

        本文标题:YYKit源码探究(二十四) —— UIPasteboard分类

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