使用AWS for ios下载图像

时间:2011-10-15 16:59:48

标签: ios for-loop amazon-web-services

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

问题是图像下载后我必须把它移到iphone的照片库。代码是: -

-(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);


    }

}

我尝试了许多方法来实现它但却无法做到。 图像阵列包含图像的名称,但我无法访问这些图像。 我尝试在图像视图中显示图像,但它也没有在那里显示..

请帮忙。 感谢。

1 个答案:

答案 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];
    }