使用AWS for ios下载图像
我不熟悉ios编程,请原谅我的错误。 我正在开发一个应用程序,需要上传和下载文件,基本上是图像文件。我使用AWS ios sdk。我能够下载图像文件并将其保存到文档文件夹(在模拟器上测试时) 问题是,一旦下载图像,我就必须将其移动到iphone的照片库中。代码是:-使用AWS for ios下载图像,ios,for-loop,amazon-web-services,Ios,For Loop,Amazon Web Services,我不熟悉ios编程,请原谅我的错误。 我正在开发一个应用程序,需要上传和下载文件,基本上是图像文件。我使用AWS ios sdk。我能够下载图像文件并将其保存到文档文件夹(在模拟器上测试时) 问题是,一旦下载图像,我就必须将其移动到iphone的照片库中。代码是:- -(IBAction)downloadButtonAction:(id)sender; { NSMutableArray *rowsToBeDownloaded = [[NSMutableArray alloc] ini
-(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);
}
}
-(iAction)下载按钮操作:(id)发送方;
{
NSMutableArray*rowsToBeDownloaded=[[NSMutableArray alloc]init];
NSMutableArray*indexPaths=[[NSMutableArray alloc]init];
int指数=0;
对于(NSNumber*RowSelectedArray中选定的行)
{
如果([rowSelected boolValue])
{
[rowsToBeDownloaded addObject:[objects objectAtIndex:index]];
NSU整数路径源[2]={0,索引};
nsindepath*path=[nsindepath indexPathWithIndexes:pathSource长度:2];
[indexPaths addObject:path];
}
索引++;
}
如果([rowsToBeDownloaded count]==0)
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“alert”消息:@“请选择要下载的文件”委托:自取消按钮:@“确定”其他按钮:无];
[警报显示];
[警报发布];
}
其他的
{
for(行中的id值stobedownload)
{
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*path=[DocumentsDirectoryStringByAppendingPathComponent:value];
AmazonS3Client*s3Client=[[AmazonS3Client alloc]initWithAccessKey:accessKey with secretKey:secretKey];
NSOutputStream*stream=[[NSOutputStream alloc]initToFileAtPath:path append:NO];
[stream ScheduleRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode];
[溪流开放];
S3GetObjectRequest*request=[[S3GetObjectRequest alloc]initWithKey:value withBucket:self.bucket];
request.outputStream=流;
[s3Client-getObject:request];
//UIImageWriteToSavedPhotosAlbum(自选图像,无,无,无);
[溪流关闭];
[溪流释放];
[请求释放];
}
}
[INDExpath释放];
[rowsToBeDeleted释放];
[自选阵营];
[自转移影像停止摄影库];
}
-(无效)转让影像停止酒店图书馆
{
//int指数=0;
//
//NSMutableArray*imgArr=[[NSMutableArray alloc]init];
//NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
//NSString*documentsDirectory=[paths objectAtIndex:0];
//NSDirectoryEnumerator*directoryEnumerator=[[NSFileManager defaultManager]枚举器路径:documentsDirectory];
//
//
//for(directoryEnumerator中的NSString*路径)
// {
//如果([[path pathExtension]isEqualToString:@“png”])
// {
//[imgArr addObject:path];
////UIImage*img=[UIImage imagename:path];
////UIImageWriteToSavedPhotosAlbum(img,nil,nil,nil);
//
// }
// }
NSMutableArray*imgArr=[[NSMutableArray alloc]init];
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSDirectoryEnumerator*directoryEnumerator=[[NSFileManager defaultManager]枚举器路径:documentsDirectory];
for(directoryEnumerator中的NSString*路径)
{
如果([[path pathExtension]isEqualToString:@“png”])
{
printf(“\n路径为%s,[path UTF8String]);
[imgArr addObject:path];
}
}
对于(inti=0;i,我终于能够解决这个问题。下面是为有需要的人编写的代码
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];
}
最后,我解决了这个问题,下面是代码。。