how to display image from documents folder in of iphone to image ViewI [duplicate]

若如初见. 提交于 2019-12-01 13:56:38

try this code:

save image in this path:

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

get image path:

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

In PictureDisplayViewController, you are loading image with imageNamed method that actually doesn't work until you have image with the same name into your bundle. you can load image like as follow...

In PictureDisplayViewController you already have path for the image than

imageView.image=  [UIImage imageWithContentsOfFile:filePathWithName];

I modify your code :

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

In PictureDisplayViewController

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

default.png is a default png pic in your bundle resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!