如何在iOS中创建.rtf和编辑?

时间:2013-04-29 10:09:16

标签: iphone ios objective-c xcode rtf

我在图像视图下有两个UIImageView和两个textfiled。 image1的一个文本字段和image2的第二个文本字段。

现在我希望该用户输入文本并提供图像名称,该图像将以.rtf格式保存在文档目录中。当我打开.rtf时,我将能够编辑该rtf。

imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100,50, 100, 100)];
[imageView setImage:[UIImage imageNamed:@"image1.png"]];
self.view addSubview:imageView];
[imageView release];

imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(100,150, 100, 100)];
[imageView1 setImage:[UIImage imageNamed:@"image2.png"]];
self.view imageView1];
[imageView1 release];




txt=[[UITextField alloc]initWithFrame:CGRectMake(5,190, 180, 30)];
txt.backgroundColor=[UIColor whiteColor];
txt.placeholder=@"Enter Text";
txt.textColor = [UIColor blackColor];
txt.delegate =self;
[txt addTarget:self action:@selector(ImageSave_method) forControlEvents:UIControlEventEditingDidEnd];
txt.textAlignment=NSTextAlignmentCenter;
txt.userInteractionEnabled = YES;
txt.autocapitalizationType = NO;
txt.font=[UIFont fontWithName:@"Helvetica" size:15];
[self.view addSubview:txt];



txt2=[[UITextField alloc]initWithFrame:CGRectMake(200,190, 180, 30)];
txt2.backgroundColor=[UIColor whiteColor];
txt2.placeholder=@"Enter Text";
txt2.textColor = [UIColor blackColor];
txt2.delegate =self;
[txt2 addTarget:self action:@selector(ImageSave_method) forControlEvents:UIControlEventEditingDidEnd];
txt2.textAlignment=NSTextAlignmentCenter;
txt2.userInteractionEnabled = YES;
txt2.autocapitalizationType = NO;
txt2.font=[UIFont fontWithName:@"Helvetica" size:15];
[self.view addSubview:txt2];

我不知道该怎么做。

非常欢迎任何想法或建议。

1 个答案:

答案 0 :(得分:0)

您可以使用nsuserdefaults保存图像名称

    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:_imageName1 forKey:@"imageName1"];
    [userDefault setObject: _imageName2 forKey:@"imageName2"];
    [userDefault synchronize];

用于从nsuserdefaults

检索数据
    NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
    NSString *_imageName1 = [userDefault objectForKey:@"imageName1"];
    NSString *_imageName2 = [userDefault objectForKey:@"imageName2"];

将图像保存在文档目录中

  1. 创建文件路径

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
        NSUserDomainMask, YES) objectAtIndex:0];
    NSString *imagePath1 =  [NSString stringWithFormat:@"%@/imageName1.png",
        docDir];
    
  2. 将UIImage转换为nsdata

    NSData *data = UIImagePNGRepresentation(myImage.image);
    
  3. 然后将图像保存到文档目录的

    的imagePath1中
    [data writeToFile:imagePath1 atomically:YES];
    
  4. 用于检索图像

    UIImage *img = [UIImage imageWithContentsOfFile:imagePath1];