我正在使用亚马逊网络服务上传图片。我无法访问GCD块中的值。我应该如何在其他类中访问“myString”的值?
我的代码:
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{
AppDelegate *del = [[UIApplication sharedApplication]delegate];
if(indicatorView!=nil)
{
[indicatorView removeFromSuperview];
indicatorView=nil;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Upload Completed" message:@"The image was successfully uploaded." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
//del.fileName = url;
// Set the content type so that the browser will treat the URL as an image.
/*S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides alloc] init];
override.contentType = @"image/jpeg";
// Request a pre-signed URL to picture that has been uplaoded.
S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init];
gpsur.key = @"NameOfThePicture";
//del.fileName = gpsur.key;
gpsur.bucket = @"pic-bucket";
gpsur.expires = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time.
gpsur.responseHeaderOverrides = override;
// Get the URL
NSError *error;
url = [self.s3 getPreSignedURL:gpsur error:&error];
NSString *myString = [url absoluteString];
NSLog(@"myStringgg====>%@",myString);
del.fileName = myString;
NSLog(@"URL=====>%@",url);*/
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^
{
// Set the content type so that the browser will treat the URL as an image.
S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides alloc] init];
override.contentType = @"image/jpeg";
// Request a pre-signed URL to picture that has been uplaoded.
S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init];
gpsur.key = @"NameOfThePicture";
//del.fileName = gpsur.key;
gpsur.bucket = @"pic-bucket";
gpsur.expires = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time.
gpsur.responseHeaderOverrides = override;
// Get the URL
NSError *error;
NSURL *url = [self.s3 getPreSignedURL:gpsur error:&error];
NSString *myString = [url absoluteString];
//del.fileName = myString;
NSLog(@"URL=====>%@",url);
if(url == nil)
{
if(error != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Error: %@", error);
//[self showAlertMessage:[error.userInfo objectForKey:@"message"] withTitle:@"Browser Error"];
});
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//del.fileName = myString;
//NSLog(@"haURL===>%@",del.fileName);
});
del.fileName = myString;
}
//NSLog(@"nameURL====>%@",url);
});
}
现在我想访问“myString”var,以便我可以在其他类中访问它的值。感谢
财产声明:
@property(nonatomic,strong)NSString * fileName; //在appdelegate.h中 @property(非原子,强)NSString *文件; //在与GCD相同的控制器中
更新
PictureViewController *pic = [[PictureViewController alloc]init];
**NSLog(@"newNameeee====>%@",pic.File);**
AppDelegate *del = [[UIApplication sharedApplication]delegate];
NSLog(@"LocatedAt====>%@",del.locatedAt);
NSLog(@"FileSize====>%d",del.fileSize);
**NSLog(@"FileName====>%@",del.fileName);**
答案 0 :(得分:0)
问题是您尝试在del.fileName
的异步部分完成之前访问request:didCompleteWithResponse:
。
尝试以下操作:稍微修改代码,将del.fileName = myString
移动到主踏板上的调度块:
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response
{
AppDelegate *del = [[UIApplication sharedApplication]delegate];
if(indicatorView!=nil)
{
[indicatorView removeFromSuperview];
indicatorView=nil;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Upload Completed" message:@"The image was successfully uploaded." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil];
[alert show];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^
{
// Set the content type so that the browser will treat the URL as an image.
S3ResponseHeaderOverrides *override = [[S3ResponseHeaderOverrides alloc] init];
override.contentType = @"image/jpeg";
// Request a pre-signed URL to picture that has been uplaoded.
S3GetPreSignedURLRequest *gpsur = [[S3GetPreSignedURLRequest alloc] init];
gpsur.key = @"NameOfThePicture";
//del.fileName = gpsur.key;
gpsur.bucket = @"pic-bucket";
gpsur.expires = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval) 3600]; // Added an hour's worth of seconds to the current time.
gpsur.responseHeaderOverrides = override;
// Get the URL
NSError *error;
NSURL *url = [self.s3 getPreSignedURL:gpsur error:&error];
NSString *myString = [url absoluteString];
//del.fileName = myString;
NSLog(@"URL=====>%@",url);
if(url == nil)
{
if(error != nil)
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Error: %@", error);
//[self showAlertMessage:[error.userInfo objectForKey:@"message"] withTitle:@"Browser Error"];
});
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
del.fileName = myString;
[del didReceivePictureFilename];
});
}
});
}
比您在didReceivePictureFilename
中声明AppDelegate
并将代码的访问部分移动到该方法。但是你必须在其他地方开始上传的部分。 (您问题中的代码未显示该部分)
当然您也可以完全省略fileName
属性并直接在didReceivePictureFilename
回调中传递文件名:
[del didReceivePictureFilename:myString];