gpt4 book ai didi

ios - 如何从 uitextview 中提取链接并从 facebook 或 twitter 等链接中加载图像?

转载 作者:可可西里 更新时间:2023-10-31 23:59:05 24 4
gpt4 key购买 nike

最佳答案

这是代码。

- (void)viewDidLoad
{
[super viewDidLoad];
NSString * testStr=@"The world’s most popular camera is more advanced than ever. The 12-megapixel iSight camera captures sharp, detailed photos. It takes brilliant 4K video, up to four times the resolution of 1080p HD video. http://cdn2.gsmarena.com/vv/bigpic/apple-iphone-6s1.jpg. iPhone 6s also takes selfies worthy of a self-portrait with the new 5-megapixel FaceTime HD camera. And it introduces Live Photos, a new way to relive your favourite memories. It captures the moments just before and after your picture and sets it in motion with just the press of a finger.";
[self identifyTextContent:testStr];
}

-(void)identifyTextContent :(NSString *)txtvwstr
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:txtvwstr];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
[detector enumerateMatchesInString:txtvwstr options:kNilOptions range:NSMakeRange(0, [txtvwstr length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSLog(@"Values: %@", result);
if (result.resultType == NSTextCheckingTypeLink)
{
NSLog(@"matched: %@",result.URL);
//**You can use [SDWebImage][1] library to download the image from detected url.**//
NSData *data = [NSData dataWithContentsOfURL:result.URL];
UIImage *img = [[UIImage alloc] initWithData:data];
textAttachment.image = img;
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:result.range withAttributedString:attrStringWithImage];
}
}];
//**HERE _txtVwData is my UITextView outlet.**
_txtVwData.attributedText=attributedString;
}

希望对您有所帮助。谢谢。

enter image description here

这是输出屏幕。

关于ios - 如何从 uitextview 中提取链接并从 facebook 或 twitter 等链接中加载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35908306/

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