0

我在图像视图下有两个 UIImageView 和两个文本文件。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];

我不知道该怎么做。

任何想法或建议都将受到高度欢迎。

4

1 回答 1

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];
    

用于检索图像

UIImage *img = [UIImage imageWithContentsOfFile:imagePath1];
于 2013-04-29T10:46:02.373 回答