gpt4 book ai didi

ios - 使用 UIPasteboard 从 UIWebView 获取图像

转载 作者:行者123 更新时间:2023-12-01 16:30:04 26 4
gpt4 key购买 nike

我正在尝试从 UIWebView 获取复制的图像使用 UIPasteboard .

[_myWebView copy:[UIApplication sharedApplication]]; //Copy selection to general pasteboard

NSArray* types = [NSArray arrayWithArray:[UIPasteboard generalPasteboard].pasteboardTypes];
NSLog(@"Type %@", [types objectAtIndex:0]);

if([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString]){

NSLog(@"text selected %@",[UIPasteboard generalPasteboard].string);

}else if([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListImage]){

NSLog(@"image selected");

}else if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListURL]){

NSLog(@"url selected");
}

如果我复制文本没关系,但如果我复制图像,我会得到:
WebController.m:445 > Type com.apple.rtfd
WebController.m:458 > text selected {\rtf1\ansi\ansicpg1252
{\fonttbl\f0\fnil\fcharset0 .SFUIText-Regular;}
{\colortbl;\red255\green255\blue255;\red45\green45\blue45;}
\deftab720
\pard\pardeftab720\qc\partightenfactor0
\f0\fs32 \cf2 \expnd0\expndtw0\kerning0
\outl0\strokewidth0 \strokec2 \
}

我尝试了来自不同网站的许多图像,但我总是得到 rtfd 类型,这实际上是富文本。有人可以帮忙吗?

最佳答案

我找到了一个解决方案:

1)在您的网页 View 中添加长按手势

UILongPressGestureRecognizer* longTap = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:nil];
longTap.delegate = self;
[_myWebView addGestureRecognizer:longTap];

2)实现委托(delegate)方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
NSLog(@"TAPPED");

if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) {

CGPoint touchPoint = [touch locationInView:self.view];

NSString *imgURL = [NSString stringWithFormat:@"document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y];
NSString *urlToSave = [_myWebView stringByEvaluatingJavaScriptFromString:imgURL];
NSURL * imageURL = [NSURL URLWithString:urlToSave];
NSLog(@"image URL :%@",urlToSave);

NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
NSLog(@"Image:%.00fx%.00f",image.size.height,image.size.width);

if (image != nil) {
NSLog(@"image exist 1");
_selectedImageDataAtPoint = imageData;

}

}
return YES;
}

其中“_selectedImageDataAtPoint”是 NSData 类型的类变量,用于存储数据图像(如果存在)

3)在用于获取文本和图像的方法中选择后检查数据是否存在
[_myWebView copy:[UIApplication sharedApplication]]; //Copy selection to general pasteboard

NSArray* types = [NSArray arrayWithArray:[UIPasteboard generalPasteboard].pasteboardTypes];
NSLog(@"Type %@", [types objectAtIndex:0]);

if ([[types objectAtIndex:0] isEqualToString:@"com.apple.rtfd"]) {

if ([UIImage imageWithData:_selectedImageDataAtPoint] != nil) {

NSLog(@"image exist 2");
_manualRecipe.imageData = _selectedImageDataAtPoint;
[self openActivityWithImage];

}

}else if([[UIPasteboard generalPasteboard] containsPasteboardTypes:UIPasteboardTypeListString]){

NSLog(@"text selected: %@",[UIPasteboard generalPasteboard].string);
NSString* text = [UIPasteboard generalPasteboard].string;
[self openActivityWithText:text];

}

关键是为了从网络上选择文本或图像,用户长按,所以我添加了另一个 UILongPressGestureRecognizer 来获取图像。之后,我展示了我的自定义 UIMenuItem 以让用户保存选择。

关于ios - 使用 UIPasteboard 从 UIWebView 获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32267818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com