如何创建包含文本和图像的自定义文件类型?

发布于 2024-11-03 07:58:48 字数 247 浏览 6 评论 0原文

我知道这有点难以回答,但我需要将文本和图像保存到一个文件中(就像 Photoshop 所做的那样)。我应该创建自定义文件类型,还是有其他方法可以做到这一点?

如果我需要自定义文件类型,有人可以给我一些可以帮助我使文件类型正确存储数据的链接吗?如果有其他方法,我该怎么做?

注意:我在导入三个图像的应用程序中使用它,并允许用户在它们上面放置文本,因此我可能无法获取以任何方式将图像存储为文本所需的信息。如果这是我必须做的,我将需要一些有用信息的链接。

I know this is going to be somewhat hard to answer, but I need to save text and images into a single file (like photoshop does). Should I make a custom file type, or is there another way to do this?

If I need a custom file type, would somebody please give me link to something that may help me make the file type store the data properly? If there is another way, how can I do this?

NOTE: I am using this in a application that imports three images, and lets users put text over them, so I may not be able to get the information needed to store the images as text in any way. If it is what I have to do, I will need a link to some helpful information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若沐 2024-11-10 07:58:48

将对象保存到文件的一种简单方法是将它们添加到 NSDictionary 中,然后使用 NSArchiver 将字典存档到数据中。

例如,在基于文档的应用程序中:

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
    if ([typeName isEqualToString:@"YourTypeName"]) {
          //Create a Dictionary
          NSMutableDictionary *dictToSave = [NSMutableDictionary dictionary];
          //Add the first image to the dictionary
          [dictToSave setObject:image1 forKey:@"Image1"];
          //Add the first image's text
          [dictToSave setObject:image1text forKey:@"Image1Text"];
          //etc.
          [dictToSave setObject:image2 forKey:@"Image2"];
          [dictToSave setObject:image2text forKey:@"Image2Text"];              
          [dictToSave setObject:image3 forKey:@"Image3"];
          [dictToSave setObject:image3text forKey:@"Image3Text"];
          //Return the archived data
          return [NSArchiver archivedDataWithRootObject:dictToSave];
    }
    //Don't generate an error
    outError = NULL;
    return nil;
}

要读取此数据,只需取消存档字典并设置对象:

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName
error:(NSError **)outError {
    if ([typeName isEqualToString:@"MyTypeName"]) {
        NSMutableDictionary *readDict = 
        [NSUnarchiver unarchiveRootObjectWithData:data];
        image1 = [readDict objectForKey:@"Image1"];
        image1text = [readDict objectForKey:@"Image1Text"];
        image2 = [readDict objectForKey:@"Image2"];
        image2text = [readDict objectForKey:@"Image2Text"];
        image3 = [readDict objectForKey:@"Image3"];
        image3text = [readDict objectForKey:@"Image3Text"];
    }
    outError = NULL;
    return YES;
}

您还需要在应用程序的 plist 文件中定义自定义文件类型。

An easy way to save objects to a file is to add them to an NSDictionary and then use NSArchiver to archive the dictionary into data.

For example in a document based app:

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
    if ([typeName isEqualToString:@"YourTypeName"]) {
          //Create a Dictionary
          NSMutableDictionary *dictToSave = [NSMutableDictionary dictionary];
          //Add the first image to the dictionary
          [dictToSave setObject:image1 forKey:@"Image1"];
          //Add the first image's text
          [dictToSave setObject:image1text forKey:@"Image1Text"];
          //etc.
          [dictToSave setObject:image2 forKey:@"Image2"];
          [dictToSave setObject:image2text forKey:@"Image2Text"];              
          [dictToSave setObject:image3 forKey:@"Image3"];
          [dictToSave setObject:image3text forKey:@"Image3Text"];
          //Return the archived data
          return [NSArchiver archivedDataWithRootObject:dictToSave];
    }
    //Don't generate an error
    outError = NULL;
    return nil;
}

To read this data, just unarchive the dictionary and set the objects:

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName
error:(NSError **)outError {
    if ([typeName isEqualToString:@"MyTypeName"]) {
        NSMutableDictionary *readDict = 
        [NSUnarchiver unarchiveRootObjectWithData:data];
        image1 = [readDict objectForKey:@"Image1"];
        image1text = [readDict objectForKey:@"Image1Text"];
        image2 = [readDict objectForKey:@"Image2"];
        image2text = [readDict objectForKey:@"Image2Text"];
        image3 = [readDict objectForKey:@"Image3"];
        image3text = [readDict objectForKey:@"Image3Text"];
    }
    outError = NULL;
    return YES;
}

You will also need to define your custom file type in your app's plist file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文