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」変数にアクセスして、他のクラスの値にアクセスできるようにします。ありがとう

プロパティ宣言:

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

で宣言し、コードのアクセス部分をそのメソッドに移動するよりdidReceivePictureFilenameAppDelegate。ただし、アップロードを開始する部分は別の場所で行う必要があります。(あなたの質問のコードはその部分を示していません)

もちろん、プロパティを完全に省略して、コールバックfileNameでファイル名を直接渡すこともできます。didReceivePictureFilename

[del didReceivePictureFilename:myString];
于 2012-10-31T14:19:40.360 に答える