UIPasteboard的代码?

时间:2016-01-15 00:06:52

标签: xcode uipasteboard

有没有办法将此代码放在UIPasteboard

[TEXTFeild setLeftViewMode:UITextFieldViewModeAlways];

UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
imageView1.image = image;
TEXTFeild.leftView = imageView1;

1 个答案:

答案 0 :(得分:0)

要将图像共享到粘贴板,请使用:

//Method 1
UIImage * image=[UIImage imageWithContentsOfFile:@"FILE_PATH"];
UIPasteboard * pasteboard=[UIPasteboard generalPasteboard];
[pasteboard setImage:image];

//Method 2
NSData *imageData = UIImagePNGRepresentation(image);
[pasteboard setData:imageData forPasteboardType:(NSString *)kUTTypePNG];

并在粘贴之前缩放图像:

CGFloat image_max_height = 1080;
CGFloat image_max_width = 1080;

UIImage *finalImage = image;

if (image.size.width>image_max_width || image.size.height>image_max_height) {
    finalImage = [image imageByScalingAspectFitSize:CGSizeMake(image_max_width, image_max_height)];
}

如果图像大于1080x1080,此代码会将图像缩小为1080px x 1080px。

更新:该方法来自名为UIImage+SimpleResize的UIImage类别。将它添加到您的项目中就可以了。