在文档目录文件夹中获取图像

在文档目录文件夹中获取图像

我想在文档目录文件夹中获取图像。

我正在使用此代码。

  NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

  for(int i=0;i<[imagepath count];i++)
  {
  NSString* documentsDirectory = [path objectAtIndex:0];

  NSURL *img = [NSURL URLWithString:[imagepath objectAtIndex:i]];

  //here imagepath is array in that i get the url of image.
  //here when i print the img data i got url from where i have to get images.


  NSData *data = [NSData dataWithContentsOfURL:img options:NSDataReadingMapped error:nil];

  //here when i put NSLog i get the null.

  NSString *fullImagePath1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",i]];

 [newreturnImage addObject:fullImagePath1];

 [data writeToFile:fullImagePath1 options:NSDataWritingAtomic error:nil];
 }


我无法在文档目录文件夹中获取图像。

此代码可能有什么问题?

最佳答案

试试这个代码,

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    NSString *docDirectory = [sysPaths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", docDirectory,@"myimage.png"];
    UIImage *cellImage=[UIImage imageWithContentsOfFile:filePath];
    UIImageView *temp=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    temp.image=cellImage;
    [myView addSubview:temp];
    [temp release];


更新 :

NSData *myData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];
UIImage *myImage = [[UIImage alloc] initWithData:myData];

关于iphone - 无法在文档目录文件夹中获取图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7397455/

10-12 07:41