2 lianlianbushell lianlianbushell 于 2013.07.29 17:18 提问

iPhone-从文件夹中显示图片

显示保存在文件夹中的图片。只返回了文件名字,没显示。

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

1个回答

Mr_me
Mr_me   2013.07.29 17:32
已采纳

保存图片路径:

 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;
Csdn user default icon
上传中...
上传图片
插入图片