如何将图像从iphone中的文件夹显示到imageView

时间:2013-07-29 05:42:03

标签: iphone ios imageview documents

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

 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 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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];

答案 2 :(得分:0)

PictureDisplayViewController中,您使用imageNamed方法加载图片,直到您的图片中包含相同名称的图片才会生效。 你可以像下面一样加载图片......

PictureDisplayViewController中,您已拥有图像的路径而不是

imageView.image=  [UIImage imageWithContentsOfFile:filePathWithName];

答案 3 :(得分: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];

图片显示ViewController

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

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