剪贴板功能

本文探讨了iOS中剪贴板的功能,包括复制粘贴字符串和图片。剪贴板能保存复杂数据并保持内容在应用关闭后仍然可用。然而,与URL Scheme相比,剪贴板的安全性较低,任何应用都能访问,可能引发安全问题。同时,剪贴板使用会消耗内存,并需要时间进行读写操作,因此在程序后台时建议清空剪贴板。
摘要由CSDN通过智能技术生成

1,复制粘贴字符串

    //这个是需要输入的信息,即是要复制的信息
    UITextField *textfild = [[UITextField alloc] init];
    self.textfild = textfild;
    textfild.placeholder = @"请输入信息";
    textfild.font = [UIFont systemFontOfSize:17];
    [textfild setTextColor:[UIColor redColor]];
    textfild.frame = CGRectMake(10, 60, [UIScreen mainScreen].bounds.size.width-20, 40);
    textfild.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textfild];
    
    //把上面复制的信息粘贴到这个控件中,用
    UITextField *textfild2 = [[UITextField alloc] init];
    self.textfild2 = textfild2;
    textfild2.placeholder = @"请输入信息2";
    textfild2.font = [UIFont systemFontOfSize:17];
    [textfild2 setTextColor:[UIColor redColor]];
    textfild2.frame = CGRectMake(10, CGRectGetMaxY(textfild.frame)+10, [UIScreen mainScreen].bounds.size.width-20, 40);
    textfild2.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textfild2];

执行下面的方法会把textfild中的信息粘贴给textfild2
/**
 共享的字符串
 */
- (void)shareNSString{
    //把信息放入粘贴板中
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    self.pasteboard = pasteboard;
    pasteboard.string = self.textfild.text;
//    pasteboard.strings = @[self.textfild.text,@"haha"];
    
    //从粘贴板中取出数据
    NSString *str = [UIPasteboard generalPasteboard].string;
//    NSArray *ar = pasteboard.strings;
//    NSString *str = [NSString stringWithFormat:@"%@,%@",ar[0],ar[1]];
    self.textfild2.text = str;
}

2,复制粘贴图片

    //要被复制的图片
    UIImageView *imageview1 = [[UIImageView alloc] init];
    self.imageview1 = imageview1;
    imageview1.frame = CGRectMake(10, CGRectGetMaxY(textfild2.frame)+10, 100, 100);
    imageview1.contentMode = UIViewContentModeScaleAspectFill;
    imageview1.clipsToBounds = YES;
    imageview1.image = [UIImage imageNamed:@"wuyifan"];
    [self.view addSubview:imageview1];
    
    //粘贴照片
    UIImageView *imageview2 = [[UIImageView alloc] init];
    self.imageview2 = imageview2;
    imageview2.frame = CGRectMake(10, CGRectGetMaxY(imageview1.frame)+20, 100, 100);
    imageview2.contentMode = UIViewContentModeScaleAspectFill;
    imageview2.clipsToBounds = YES;
    [self.view addSubview:imageview2];
执行下面的方法把复制的图片粘贴到imageview2上
/**
 共享data数据
 */
- (void)shareData{
    //把信息放入粘贴板
    UIPasteboard *pastedboard = [UIPasteboard generalPasteboard];
    NSData *data = UIImageJPEGRepresentation(self.imageview1.image, 1.0);
    [pastedboard setData:data forPasteboardType:@"imagedata"];
    //从粘贴板中取出数据
    NSData *data2 = [pastedboard dataForPasteboardType:@"imagedata"];
    UIImage *image2 = [UIImage imageWithData:data2];
    self.imageview2.image = image2;
}

3,剪贴板和 URL Scheme之间的比较

剪贴板:它具有支持复杂数据(如图片)的能力;支持多种形式表示数据;在应用关闭后,剪贴板内容仍然会被保留。

与URL Scheme比较,它不能够判断是否安装了目标app;任何应用都可以访问剪贴板,会带来安全问题;

剪贴板不仅消耗内存,也需要额外的时间来读写,程序进入后台等状态时,应当清除剪贴板,pastedboard.items = nil;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值