【问题标题】:How to retrieve photo from UITextView after adding it using NSAttributedString?使用 NSAttributedString 添加后如何从 UITextView 中检索照片?
【发布时间】:2015-07-22 01:05:34
【问题描述】:

我正在使用下一个代码将图像添加到 UITextView:

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(200,200,140,140)];
    textView.font = [UIFont systemFontOfSize:20.0f];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test  with emoji"];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"Angel.png"];

    //for the padding inside the textView
    textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:3.0 orientation:UIImageOrientationUp];
    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attrStringWithImage];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, attributedString.length)];
    textView.attributedText = attributedString;
    NSLog(@"Text view: %@", textView.attributedText);

    [self.view addSubview:textView];

结果如下:

我感兴趣的是,我如何知道在文本字段中插入了什么图片以及在女巫位置? 正如您在代码中观察到的那样,我正在考虑使用属性文本,因为它会记录:

Text view: Test {
    NSFont = "<UICTFont: 0x7ff0324f2110> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
}{
    NSAttachment = "<NSTextAttachment: 0x7ff032682bc0>";
    NSFont = "<UICTFont: 0x7ff0324f2110> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
}with emoji{
    NSFont = "<UICTFont: 0x7ff0324f2110> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
} 

更新

使用代码检索图像:

NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
    [attributedString enumerateAttribute:NSAttachmentAttributeName
                                 inRange:NSMakeRange(0, [attributedString length])
                                 options:0
                              usingBlock:^(id value, NSRange range, BOOL *stop)
    {
        if ([value isKindOfClass:[NSTextAttachment class]])
        {
            NSTextAttachment *attachment = (NSTextAttachment *)value;
            UIImage *image = nil;
            if ([attachment image])
                image = [attachment image];
            else
                image = [attachment imageForBounds:[attachment bounds]
                                     textContainer:nil
                                    characterIndex:range.location];

            if (image)
                [imagesArray addObject:image];
        }
    }];

但是如果属性字符串包含多于 1 张连续照片怎么办? 示例:

  • 如果字符串包含两张连续的照片,那么只有一张被添加到数组中 外观

代码

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test   with emoji "];
    [attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
    [attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attrStringWithImage];

日志:

Image array: (
    "<UIImage: 0x7fd4e3e56760>"
)
  • 如果照片不连续,则它们都被正确添加到数组中 外观

代码

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test  with emoji "];
[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
    [attributedString replaceCharactersInRange:NSMakeRange(16, 1) withAttributedString:attrStringWithImage];

日志

Image array: (
    "<UIImage: 0x7f9ce35a4a70>",
    "<UIImage: 0x7f9ce35a4a70>"
)

那么,我正在做的事情或 enumerateAttribute 方法是否存在错误?

更新 2 如果我为我添加的每张照片创建一个新的 textAttachmentattrStringWithImage 实例,则设法解决了这个问题。

【问题讨论】:

  • 枚举你的NSAttributedString。在这里,我只保留了图像,但您也可以保存块中给出的rangestackoverflow.com/questions/29152660/…
  • @Larme 您能否制定一个答案,并包括我的第二个问题的答案,即如果有多个图像,如何获取所有图像?提前谢谢!

标签: ios objective-c uitextview nsattributedstring


【解决方案1】:

检索图像解释here

您的新问题是,如果两个图像是连续且相同的。

所以而不是:

if (image)
    [imagesArray addObject:image];

您需要进行其他检查,这将适用于两个图像,但您无法知道它们是否连续。

if (image)
{
    if ([imagesArray lastObject] != image)
        [imagesArray addObject:image];
}

所以你也需要保留NSRange 的引用。

if (image)
{
    if ([imagesArray count] > 0)
    {
        NSDictionary *lastFound = [imagesArray lastObject];
        NSRange lastRange = [lastFound[@"range"] rangeValue];
        UIImage *lastImage = lastFound[@"image"];
        if (lastImage == image && lastRange.location+lastRange.length == range.location)
        { //Two images same & consecutive}
        else
        {
            [imagesArray addObject:@{@"image":image, @"range":[NSValue valueWithRange:range]}];
        }
    }
    else
    {
        [imagesArray addObject:@{@"image":image, @"range":[NSValue valueWithRange:range]}];
    }
}

仅检索图像:

NSArray *onlyImages = [imagesArray valueForKey:@"image"];

注意:我没有检查这段代码是否编译,但你应该明白整个想法。 我的范围计算可能是错误的(缺少一些 +1/-1,但没有什么难以通过测试验证),如果两个相同的连续图像之间有空间怎么办?您可能希望获取 (NSString *stringBetween = [[attributedString string] substringWithRange:NSMakeRange(lastRange.location+lastRange.length, range.location-lastRange.location+lastRange.length)] 之间的字符串,并检查空格、标点符号(有很多方法可以做到)。

补充说明: 在您的情况下,仅比较image != newImage 可能就足够了,但是如果您使用网络图像,或者甚至在您的捆绑包中使用两个名称不同但相同的图像,那么知道它们是否相同是另一个问题。关于比较两张图片有一些关于 SO 的问题,但这需要一些时间/资源。

【讨论】:

  • 我还有一个问题:D 如何判断图像在 NSMutableAttributedString 中的位置?我问是因为用户可以对文本等进行一些更改,所以有没有一种方法可以轻松检索 NSMutableAttributedString 中图像的位置?
  • 职位是什么意思?这可能是另一个问题/话题。
  • 例如,在我最初的示例中,我在索引 5 处插入图像,所以我的问题是:如何检索图像所在的位置/索引?当然,如果用户对字符串进行一些更改,索引将不再位于索引 5
  • if (image == imageWanted){NSLog(@"NewRange: %@", NSStringFromRange(range));}代替if (image) [imagesArray addObject:image];
  • 您正在寻找的图像。否则,您可以在我的答案开始时保留imageArray 的副本,并将范围/图像与您在再次枚举属性字符串时找到的范围/图像进行比较。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
相关资源
最近更新 更多