0

我正在使用亚马逊网络服务上传图片。我无法访问 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(nonatomic,strong) NSString *File; //与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);**
4

1 回答 1

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];
于 2012-10-31T14:19:40.360 回答