0

嗨朋友,我正在将图像保存在文档目录中,就像在文档目录中一样,图像像 saveimage0.png,saveimage1.png ......等这样的图像被输入到文档中,我创建 int 变量用于增量计数,图像 num=0;插入工作正常但是如何从 loadimage menthod 中的文档文件夹获取多个图像是对还是错我想要 tkae 图像并保存在数组中并显示在视图中

int num=0;
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{


    NSString *mediaType = [info
                           objectForKey:UIImagePickerControllerMediaType];
    [self dismissModalViewControllerAnimated:YES];
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info
                          objectForKey:UIImagePickerControllerOriginalImage];

        imageView.image = image;
        UIImage *image1 =imageView.image;
        NSData *myData = [UIImagePNGRepresentation(image1) retain];
        imagedata = myData;


        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,  
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                             NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"saveimage%d.png",num]];
        num += 1; // for next t
        NSData* data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];



    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
    {
        // Code here to support video if enabled
    }
}

我正在从文档目录中获取图像,与

int value;
- (UIImage*)loadImage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
       NSString *documentsDirectory = [paths objectAtIndex:0];


    // NSString* path = [documentsDirectory stringByAppendingPathComponent: 
   //[NSString stringWithString: @"test.png"] ];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithFormat:  @"savedImage-",@"%@-%d.png", value]];

    value += 1;

    UIImage* image = [UIImage imageWithContentsOfFile:path];

    return image;


}
4

1 回答 1

2

答案在你的问题之内。

您正在使用以下方式保存图像:

NSString *path = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"saveimage%d.png",num]];

格式为:saveimage0.png

你正在检索:

NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithFormat:  @"savedImage-",@"%@-%d.png", value]];

格式为:savedImage-aValue-aValue.png.

将其更改为:

 NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                          [NSString stringWithFormat:  @"savedimage%d.png", value]];
于 2013-01-02T05:48:53.757 回答