2014-01-13 112 views
0

我已经使用以下代码创建了名为“Image store”的文件夹。我的要求是我想保存图像到文件夹“图像存储”在api成功和图像应保存在应用程序本身不在数据库或相册。我想知道我可以在应用程序中存储图像的机制在应用程序中存储图像

-(void) createFolder { 

    UIImage *image = [[UIImage alloc]init]; 

    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/ImageStore"]; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 
    else 
    { 

    } 
} 

回答

1

ü可以做这样的事情 U可以运行图像的循环这样

//at this point u can get image data 
    for(int k = 0 ; k < imageCount; k++) 
    { 
    [self savePic:[NSString stringWithFormat:@"picName%d",k] withData:imageData];//hear data for each pic u can send 
    } 


- (void)savePic:(NSString *)picName withData:(NSData *)imageData 
    { 
    if(imageData != nil) 
    { 
     NSString *path = [NSString stringWithFormat:@"/ImageStore/%@.png",pincName]; 
     NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
     NSString *pngPath = [NSString stringWithFormat:@"%@%@",Dir,path]; //path means ur destination contain's this format -> "/foldername/picname" pickname must be unique 
    if(![[NSFileManager defaultManager] fileExistsAtPath:[pngPath stringByDeletingLastPathComponent]]) 
    { 
     NSError *error; 
     [[NSFileManager defaultManager] createDirectoryAtPath:[pngPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:&error]; 
     if(error) 
     { 
      NSLog(@"error in creating dir"); 
     } 
    } 
    [imageData writeToFile:pngPath atomically:YES]; 
    } 
} 


成功下载后保存u能获取相似图片下面


- (UIImage *)checkForImageIn:(NSString *)InDestination 
    { 

    NSString *Dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *pngPath = [NSString stringWithFormat:@"%@%@",Dir,InDestination];//hear "InDestination" format is like this "/yourFolderName/imagename" as i said imagename must be unique .. :) 


    UIImage *image = [UIImage imageWithContentsOfFile:pngPath]; 
    if(image) 
    { 
     return image; 
    } 
    else 
    return nil; 
} 


link to find path 看到这个链接找到的路径..

aganin做相同的这运行循环如下

NSMutableArray *imagesArray = [[NSMutableArray alloc]init]; 
    for(int k = 0 ; k < imageCount; k++) 
    { 
     UIImage *image = [self checkForImageIn:[NSString stringWithFormat: @"/yourFolderName/ImageName%d",k]];//get the image 
     [imagesArray addObject:image];//store to use it somewhere .. 
    } 
+0

我有超过十个images.how我可以做到这一点吗? – arshad

+0

看到更新的答案,评论如果你面临任何问题 –

+0

我怎么知道图像是否存储在文件夹中? – arshad

1

写入创建目录此代码后

NSString *path= [documentsDirectory stringByAppendingPathComponent:@"/ImageStore"]; 
UIImage *rainyImage =[UImage imageNamed:@"rainy.jpg"]; 
NSData *Data= UIImageJPEGRepresentation(rainyImage,0.0); 
[data writeToFile:path atomically:YES] 
+0

[数据writeToFile:原子路径:是]什么是'路径'在这里?或者它应该像[数据writeToFile:datapath atomically:是] – arshad

+0

是使用数据路径 –

1

的文件目录中找到这样的:

// Let's save the file into Document folder. 
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

// If you go to the folder below, you will find those pictures 
NSLog(@"%@",docDir); 

NSLog(@"saving png"); 
NSString *pngFilePath = [NSString stringWithFormat:@"%@/test.png",docDir]; 

那只是一个提供的代码,告诉您正确的路径是在你的IPONE设备保存的样品。

查看下面的博客文章,它是一步一步的指导与源代码。

Download an Image and Save it as PNG or JPEG in iPhone SDK

2
//Make a method that has url (fileName) Param 
NSArray *documentsDirectory = 
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES); 

NSString *textPath = [documentsDirectory stringByAppendingPathComponent:url]; 

NSFileManager *fileManager =[NSFileManager defaultManager]; 

if ([fileManager fileExistsAtPath:textPath]) 
{ 
    return YES; 
} 
else 
{ 
    return NO; 
} 

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage 
imageNamed:@""]];//Placeholder image 


if ([url isKindOfClass:[NSString class]]) 
{ 
    imgView.image = [UIImage imageNamed:[url absoluteString]]; 
    imgView.contentMode = UIViewContentModeScaleAspectFit; 
} 
else if ([fileManager fileExistsAtPath:url]) 
{ 


    NSString *textPath = [documentsDirectory stringByAppendingPathComponent:url]; 

    NSError *error = nil; 

    NSData *fileData = [NSData dataWithContentsOfFile:textPath options:NSDataReadingMappedIfSafe error:&error]; 

    if (error != nil) 
    { 
     DLog(@"There was an error: %@", [error description]); 
     imgView.image=nil; 
    } 
    else 
    { 
     imgView.image= [UIImage imageWithData:fileData] 
    } 
} 
else 
{ UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] 
    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 


    CGPoint center = imgView.center; 
    //   center.x = imgView.bounds.size.width/2; 

    spinner.center = center; 
    [spinner startAnimating]; 

    [imgView addSubview:spinner]; 


    dispatch_queue_t downloadQueue = dispatch_queue_create("iamge downloader", NULL); 

    dispatch_async(downloadQueue, ^{ 

     NSData *imgData = [NSData dataWithContentsOfURL:url]; 

     dispatch_async(dispatch_get_main_queue(), ^{ 


      [spinner removeFromSuperview]; 

      UIImage *image = [UIImage imageWithData:imgData]; 


      NSError *error = nil; 

[imgData writeToFile:url options:NSDataWritingFileProtectionNone error:&error]; 

if (error != nil) 
{ 

} 
else 
{ 

} 

      imgView.image = image; 
     }); 
    }); 
} 

那的UIImageView加载图像,如果它不列入存在于文件然后保存,活动指示器被添加到显示图像被加载到保存,

相关问题