gpt4 book ai didi

ios - 使用 AWS for ios 下载图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:09:03 24 4
gpt4 key购买 nike

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

问题是一旦图像被下载,我必须将它移动到 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);


}

}

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

请帮忙。谢谢。

最佳答案

最后我解决了这个问题。这里是给有需要的人的代码。

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

关于ios - 使用 AWS for ios 下载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7779264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com