我正在从相机中捕捉图像。我正在编写捕获图像的文件名&之后,我从gallery中获取最后保存的图像。现在我正在使用blocks来做这件事。在块之外我能够访问teh变量但是在块内部我无法访问变量。请告诉我是什么问题
- (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];
}
}
代码崩溃的代码
-(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;
}
}