2011-10-15 53 views
3

我是ios编程的新手,所以请原谅我的错误。 我正在开发一个应用程序,需要上传和下载文件基本上是图像文件。我使用的是AWS ios sdk。我可以下载图像文件并将其保存到文档文件夹(在模拟器上测试时)。使用AWS为ios下载图像

的问题是,一旦图像被下载我必须将它移动到iphone.The代码照片库: -

-(IBAction)downloadButtonAction:(id)sender; 
{ 


    NSMutableArray *rowsToBeDownloaded = [[NSMutableArray alloc] init]; 
    NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; 
    int index = 0; 


    for (NSNumber *rowSelected in selectedArray) 
    { 
     if ([rowSelected boolValue]) 
     { 

      [rowsToBeDownloaded addObject:[objects objectAtIndex:index]]; 
      NSUInteger pathSource[2] = {0, index}; 
      NSIndexPath *path = [NSIndexPath indexPathWithIndexes:pathSource length:2]; 

      [indexPaths addObject:path]; 
     }  
     index++; 

    } 

    if([rowsToBeDownloaded count]==0) 
    { 
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:@"Please select a file to download" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
    else 
    { 

     for (id value in rowsToBeDownloaded) 
     { 
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString *documentsDirectory = [paths objectAtIndex:0]; 
      NSString *path = [documentsDirectory stringByAppendingPathComponent:value]; 

     AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:accessKey withSecretKey:secretKey]; 

     NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:path append:NO]; 
     [stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
     [stream open]; 

     S3GetObjectRequest *request = [[S3GetObjectRequest alloc] initWithKey:value withBucket:self.bucket]; 
     request.outputStream = stream; 
     [s3Client getObject:request]; 

     //UIImageWriteToSavedPhotosAlbum(self.selectedImage, nil,nil,nil); 

     [stream close]; 
     [stream release]; 
     [request release]; 
     } 
    } 
    [indexPaths release]; 
    [rowsToBeDeleted release]; 
    [self populateSelectedArray]; 
    [self transferImagesToPhotolibrary]; 


} 



-(void)transferImagesToPhotolibrary 
{ 
    //int index=0; 
// 
// NSMutableArray*imgArr=[[NSMutableArray alloc] init]; 
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
// NSString *documentsDirectory = [paths objectAtIndex:0]; 
// NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; 
// 
// 
// for (NSString *path in directoryEnumerator) 
// { 
//  if([[path pathExtension]isEqualToString:@"png"]) 
//  { 
//   [imgArr addObject:path];   
//    //UIImage*img=[UIImage imageNamed:path]; 
//    //UIImageWriteToSavedPhotosAlbum(img, nil,nil,nil); 
// 
//  } 
// } 


    NSMutableArray*imgArr=[[NSMutableArray alloc] init]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; 

    for (NSString *path in directoryEnumerator) 
    { 

     if([[path pathExtension]isEqualToString:@"png"]) 
     { 
      printf("\n path is %s",[path UTF8String]); 

      [imgArr addObject:path];   
     } 

    } 
    for(int i=0;i<[imgArr count];i++) 
    { 

    UIImage*img=[UIImage imageNamed:[imgArr objectAtindex:i]]; 

     UIImageWriteToSavedPhotosAlbum(img, nil,nil,nil); 


    } 

} 

我试了很多方法来实现它,但无法做到这一点。 图像数组包含图像的名称,但我无法访问这些图像。 我试图在图像视图中显示图像,但它也没有appering那里..

请帮助。 谢谢。

+0

最后,我能够解决这个问题,这里是代码.. –

回答

3

最后我能解决这个问题。这里是需要帮助的人的代码。

AmazonS3Client *s3Client = [[AmazonS3Client alloc] initWithAccessKey:accessKey withSecretKey:secretKey]; 
    NSOutputStream *stream = [[NSOutputStream alloc] initToFileAtPath:path append:NO]; 
    [stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [stream open]; 

    S3GetObjectRequest *request = [[S3GetObjectRequest alloc] initWithKey:value withBucket:self.bucket]; 
    request.outputStream = stream; 
    [s3Client getObject:request]; 


//   NSMutableArray*imgArr=[[NSMutableArray alloc] init]; 
//   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
//   NSString *documentsDirectory = [paths objectAtIndex:0]; 

     NSString *appFile = [documentsDirectory stringByAppendingPathComponent:value]; 
     NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease]; 
     UIImage *img=[UIImage imageWithData:myData]; 
     UIImageWriteToSavedPhotosAlbum(img, nil,nil,nil); 


    [stream close]; 
    [stream release]; 
    [request release]; 
    }