0

I am capturing an image from camera.I am writing the filename of captured image & after that i am getting the last saved image from the gallery.Now i am doing this thing using blocks.Outside the block i am able to access teh variable but inside block i am not able to access the variable.Please tell me what is the issue?

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    if (picker == fileBroswePicker)
    {
      chosenImage = info[UIImagePickerControllerOriginalImage];
      [[AppSettings sharedAppSettings]setImage:chosenImage];
      refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
      if (refURL == nil)
      {
        __block PHObjectPlaceholder *placeholder;
        PHPhotoLibrary *objPhoto = [PHPhotoLibrary sharedPhotoLibrary];
        [objPhoto performChanges:^{

        PHAssetChangeRequest *assets = [PHAssetChangeRequest creationRequestForAssetFromImage:info[UIImagePickerControllerOriginalImage]];
        PHObjectPlaceholder *placeholder = assets.placeholderForCreatedAsset;

        } completionHandler:^(BOOL success, NSError * _Nullable error) {

          PHAsset *asset = nil;
          PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
          fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
          PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions];
          if (fetchResult != nil && fetchResult.count > 0) {
            // get last photo from Photos
            asset = [fetchResult lastObject];
          }

          if (asset) {
            // get photo info from this asset
            PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
            imageRequestOptions.synchronous = YES;
            [[PHImageManager defaultManager]
             requestImageDataForAsset:asset
             options:imageRequestOptions
             resultHandler:^(NSData *imageData, NSString *dataUTI,
                             UIImageOrientation orientation,
                             NSDictionary *info)
             {
               NSLog(@"info = %@", info);
               if ([info objectForKey:@"PHImageFileURLKey"]) {
                 // path looks like this -
                 // file:///var/mobile/Media/DCIM/###APPLE/IMG_####.JPG
                 NSURL *path = [info objectForKey:@"PHImageFileURLKey"];
                 refURL = path;
                  NSString *strUrl  = refURL.absoluteString;
                  NSArray *parts  = [strUrl componentsSeparatedByString:@"/"];
                  orignalName = fileName = [parts lastObject];

                 fileName = [[[Constant sharedInstance]getCommanFunctionInstance]generateFileNameWithExtension:filextension];
                 NSLog(@"filename is %@",fileName);
                 [[AppSettings sharedAppSettings]setFileExtension:filextension];
                 [[AppSettings sharedAppSettings]setFileName:fileName];
                 [[AppSettings sharedAppSettings]setOrignalFileName:orignalName];
                 dispatch_async(dispatch_get_main_queue(), ^{

                   NSLog(@"picker dismissed %@",fileName);
                   [picker dismissViewControllerAnimated:YES completion:NULL];
                   [picker release];


                 });

                 [self validateImage:2];

               }                                            
             }];
          }

        }];

      }
      else
      {
        PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
        orignalName = fileName = [[result firstObject] filename];
        [[AppSettings sharedAppSettings]setOrignalFileName:orignalName];
        NSArray *parts = [fileName componentsSeparatedByString:@"."];
        if (parts.count > 0)
        {
          filextension = [parts lastObject];
          filextension  = [[@"." stringByAppendingString:filextension]lowercaseString];
          [[AppSettings sharedAppSettings]setFileExtension:filextension];

        }

        fileName = [[[Constant sharedInstance]getCommanFunctionInstance]generateFileNameWithExtension:filextension];
        [[AppSettings sharedAppSettings]setFileName:fileName];
        [picker dismissViewControllerAnimated:YES completion:NULL];
        [picker release];
        [self validateImage:1];
      }

    }

    else
  {
    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];

    [picker dismissViewControllerAnimated:YES completion:NULL];

    [picker release];

    [UIView animateWithDuration:0.5f animations:^{
      pickerView.frame = CGRectMake(10, 10, pickerView.frame.size.width, pickerView.frame.size.height);
      self.pickerImageView.image = chosenImage;
      [self.opaqeViewFull setAlpha:0.3];
      [self.opaqeViewFull setFrame:self.view.frame];
      [self.view bringSubviewToFront:self.opaqeViewFull];
      [self.view bringSubviewToFront:self.pickerView];
    }];

    [self.pickerViewSendButton setEnabled:YES];
    [self.pickerViewRightRotateButton setEnabled:YES];
    [self.pickerViewLeftRotateButton setEnabled:YES];
  }

}

Code where crash occuer is below

 -(void)validateImage:(NSInteger)type
    {
      PHFetchResult *result;
      [viewImport showFileName];

      NSLog(@"outside filename is  = %@",fileName);
      viewImport.lblFilename.text = [@"Filename:" stringByAppendingString:fileName];

      dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"inside filename is  = %@",fileName);
        if (viewImport == nil)
        {
          NSLog(@"viewimport is nill");
        }

      });
      if (fileName.length >0)
      {
        NSArray *temp = [fileName componentsSeparatedByString:@"."];
        if (temp.count > 1)
        {
          filextension  = [[@"." stringByAppendingString:temp[1]]lowercaseString];

          if (![listOfFileExtenstion containsObject:filextension])
          {
            if ([[AppSettings sharedAppSettings] getLanguageID] == 2)
            {
              [[[Constant sharedInstance]getCommanFunctionInstance]showAlert:kWarning withMessage:kfileExtenstionAlert onViewController:self
               ];

            }
            else
            {
              [[[Constant sharedInstance]getCommanFunctionInstance]showAlert:arKWarning withMessage:arKfileExtenstionAlert onViewController:self
               ];
            }


            return;
          }
        }
        else
        {
          if ([[AppSettings sharedAppSettings] getLanguageID] == 2)
          {
            [[[Constant sharedInstance]getCommanFunctionInstance]showAlert:kWarning withMessage:kinvalidFileExtenstionAlert onViewController:self
             ];
          }
          else
          {
            [[[Constant sharedInstance]getCommanFunctionInstance]showAlert:arKWarning withMessage:arKinvalidFileExtenstionAlert onViewController:self
             ];

          }
          return;
        }
      }
      else
      {
        return;
      }

    }
iOSGuy
  • 171
  • 1
  • 14
  • Possible duplicate of [Assign a variable inside a Block to a variable outside a Block](http://stackoverflow.com/questions/7962721/assign-a-variable-inside-a-block-to-a-variable-outside-a-block) –  Mar 20 '17 at 05:23
  • I have flagged this as a duplicate. If you don't think this is a duplicate, you should rephrase your question and specify exactly what you are trying and what goes wrong. I tried to read our code and got all dizzy trying to figure out what you are doing, put some effort into the details , clear out code thats not needed in the question and maybe we can find the issue. However if you found the solution in the duplicate thats even better. –  Mar 20 '17 at 05:30

0 Answers0