【问题标题】:how to display image from documents folder in of iphone to image ViewI [duplicate]如何从iphone的文档文件夹中显示图像到图像视图[重复]
【发布时间】:2013-07-28 19:30:48
【问题描述】:

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

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

【问题讨论】:

    标签: iphone ios imageview documents


    【解决方案1】:

    我修改了你的代码:

         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 图片

    【讨论】:

    • 我正在像 PicDisplayViewController 中直接编辑的代码一样,但它仍然不显示图像
    • 请确定:您的filePath 中是否有png? fileExistsYES 还是 NO?
    • 它显示没有文件,但我正在保存文件
    【解决方案2】:

    试试这个代码:

    在此路径中保存图片

     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;
    

    【讨论】:

    • 这也没有在 imageView 中显示图像
    • 检查我的更新答案
    • 我正在保存您提供的文件,但它没有加载可能是什么问题
    • 我希望在新的 NSString 中而不是显示整个 url 我只想在其他 NSString 中获取 j.png
    【解决方案3】:

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

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

    imageView.image=  [UIImage imageWithContentsOfFile:filePathWithName];
    

    【讨论】:

    • 我在 PictureDisplayViewController 中像 kirthi 一样做了,但它不起作用
    【解决方案4】:
    //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];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 2014-03-30
      • 1970-01-01
      相关资源
      最近更新 更多