0

我想显示保存在文档文件夹中的图像问题是它返回文件名但它没有在图像视图中显示图像

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

            NSString*test=@"nicej";

           patientlastName=[test stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString * filePath = [NSString stringWithFormat:@"%@/Documents//%@.png", NSHomeDirectory(),patientlastName];


        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
        if (fileExists == 0) 
        { 
            NSLog(@"File is Not exists");

            appDelegate.imageURL=@"File Not Exists";

        }

        NSString *userfile=[NSString stringWithFormat:@"%@",patientlastName];
        appDelegate.imageURL=userfile;

           PictureDisplayViewController *targetController = [[PictureDisplayViewController alloc] initWithNibName:@"PictureDisplayViewController" bundle:nil];
        targetController.modalPresentationStyle = UIModalPresentationFullScreen;
        [self.splitViewController presentViewController:targetController animated:YES completion:nil];

在 PictureDisplayViewController 中

           imageView.image=[UIImage imageNamed:@"nicej.png"];
4

4 回答 4

1

试试这个代码:

将图像保存在此路径中

 NSFileManager *fileMan = [NSFileManager defaultManager];

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

    NSString *str= [NSString stringWithFormat:@"%@.png",patientlastName];
   NSString *Image_filePath=[documentsDirectory stringByAppendingPathComponent:str];
 NSData *imagedata=UIImagePNGRepresentation(yourimage);
 [fileMan createFileAtPath:[Image_filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] contents:imagedata attributes:nil];

获取图片路径

NSString* fileName = [NSString stringWithFormat:@"%@.png",patientlastName}
        NSArray *arrayPaths =
        NSSearchPathForDirectoriesInDomains(
                                            NSDocumentDirectory,
                                            NSUserDomainMask,
                                            YES);
        NSString *path = [arrayPaths objectAtIndex:0];
        NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

      UIImage *image1=[UIImage imageWithContentsOfFile:pdfFileName];
       imageView.image=image1;
于 2013-07-29T05:47:40.953 回答
0
//suppose  imgpath is path of your image in doument directory then use these two lines of code.

NSData *imgData = [NSData dataWithContentsOfFile:imgPath];

imageView.image = [[UIImage alloc] initWithData:imgData];
于 2013-07-29T05:47:40.063 回答
0

在中,您正在使用实际上不起作用PictureDisplayViewController的方法加载图像,直到您将具有相同名称的图像放入您的包中。imageNamed你可以像下面这样加载图像...

PictureDisplayViewController您已经有了图像的路径而不是

imageView.image=  [UIImage imageWithContentsOfFile:filePathWithName];
于 2013-07-29T05:52:01.033 回答
0

我修改你的代码:

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

                NSString*test=@"nicej";

               patientlastName=[test stringByReplacingOccurrencesOfString:@" " withString:@""];
            NSString * filePath = [NSString stringWithFormat:@"%@/Documents//%@.png", NSHomeDirectory(),patientlastName];


            BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
            if (fileExists == 0) 
            { 
                NSLog(@"File is Not exists");

                appDelegate.imagePath=@"";

            }
            else
            {
                appDelegate.imagePath=filePath;
            }

               PictureDisplayViewController *targetController = [[PictureDisplayViewController alloc] initWithNibName:@"PictureDisplayViewController" bundle:nil];
            targetController.modalPresentationStyle = UIModalPresentationFullScreen;
            [self.splitViewController presentViewController:targetController animated:YES completion:nil];

在 PictureDisplayViewController 中

if(appDelegate.imagePath)
{
    UIImage *image=[UIImage imageWithContentsOfFile:appDelegate.imagePath];
}
else
{
    imageView.image=[UIImage imageNamed:@"default.png"];
}

default.png是捆绑资源中的默认 png 图片

于 2013-07-29T06:01:48.470 回答