无法获取用相机捕获的图像的文件名 [英] Unable to get File name of image captured with camera

查看:534
本文介绍了无法获取用相机捕获的图像的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


你好,我试图建立一个应用程序,我可以捕获
图像与相机和保存到图库。但我无法获取文件
的图像名称。如果我从相机胶卷中选择图像,那么我可以
获取所选图像的文件名。但是当我在我的应用程序中使用相机捕获图像
时,它返回文件名 null 。这是我的代码保存和
从图库使用 UIImagePickerController

Hello there, I'm trying to build an app in which I'm able to capture image with camera and save to gallery. But I'm unable to get the file name of image. If I select image from camera roll, then I'm able to get file name of selected image.But when I capture image with camera in my app, It returns file name "null". Here is my code to save and select image from gallery using UIImagePickerController



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

    UIImage *chosenImage = [self scaleAndRotateImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
    NSString *mediaType = info[UIImagePickerControllerMediaType];
       self.userProfileImage.image = chosenImage;

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = info[UIImagePickerControllerOriginalImage];

        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image,
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }

    imageRotation=[NSString stringWithFormat:@"%f %f %f",acos (self.userProfileImage.transform.a), asin (self.userProfileImage.transform.b), atan2(self.userProfileImage.transform.b, self.userProfileImage.transform.a)];
    CGFloat angle = [(NSNumber *)[self.userProfileImage valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
    NSLog(@"%f", angle);

    NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibrary *assetLibrary = [ALAssetsLibrary new];

    [assetLibrary assetForURL:referenceURL
                  resultBlock:^(ALAsset *asset) {
                      ALAssetRepresentation *assetRep = [asset defaultRepresentation];
                     fileName = [assetRep filename];
                      NSLog(@"File name = %@", fileName);    
                  }

                 failureBlock:^(NSError *error) {
                     NSLog(@"%@", error);
                 }];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [picker dismissViewControllerAnimated:YES completion:nil];
    imageChanged=TRUE;
}




p>

And this code to save captured image



- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alert;
    //NSLog(@"Image:%@", image);
      if (error) {
        alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                           message:[error localizedDescription]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alert show];

    }
}




我坚持的是,如何获取捕获的图像的文件名。
我在我的代码中做了什么错误?请给我建议任何更正或
解决方案。任何帮助将不胜感激。提前感谢

I'm stuck at the point that how can I get file name of captured image. What wrong I'm doing in my code ? Please suggest me any correction or solution. Any help would be appreciated . Thanks in advance


推荐答案

这里是如何获取文件,视频或照片的文件名,它使用UIImagePickerController选择:

Here is how I get file name of a file, video or photo, which picked with UIImagePickerController:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    @try {
        self.myinfo = info;
        DDLogDebug(@"MediaListView - Dismissing camera ui...");
        [self.cameraUI dismissViewControllerAnimated:YES completion:nil];

        mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
        DDLogDebug(@"MediaListView - Media url = %@", mediaURL);

        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        DDLogDebug(@"MediaListView - Selected mediaType: %@", mediaType);

        // This is a video
        if(mediaURL) {
            DDLogDebug(@"MediaListView - This is a video");

            // Just recorded video
            if (self.source == UIImagePickerControllerSourceTypeCamera) {
                DDLogDebug(@"MediaListView - This is a new video, saving to photos album...");

                // Save video before getting its name
                ALAssetsLibrary *library = [ALAssetsLibrary new];

                [library writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error){
                    if (error) {
                        DDLogDebug(@"MediaListView - Failed to save the photo to photos album...");
                    } else {
                        DDLogDebug(@"MediaListView - Video saved to photos album...");

                        // Video saved, we can get its name
                        [self getNameFromUrl:assetURL];
                    }
                }];
            }
            else {
                DDLogDebug(@"MediaListView - This is an existing video, getting name...");
                // Get video name
                [self getNameFromUrl:[info objectForKey:UIImagePickerControllerReferenceURL]];
            }
        }
        // This is a photo
        else {
            DDLogDebug(@"MediaListView - This is a photo...");
            self.originalImage = (UIImage*)[info objectForKey:UIImagePickerControllerOriginalImage];

            // Just taken photo
            if (self.source == UIImagePickerControllerSourceTypeCamera) {
                DDLogDebug(@"MediaListView - This is a new photo, saving to photos album...");

                // Save photo to album
                ALAssetsLibrary *library = [ALAssetsLibrary new];

                [library writeImageToSavedPhotosAlbum:[self.originalImage CGImage]
                                          orientation:(ALAssetOrientation)[self.originalImage imageOrientation]
                                      completionBlock:^(NSURL *assetURL, NSError *error){
                    if (error) {
                        DDLogDebug(@"MediaListView - Failed to save the vide to photos album...");
                    } else {
                        DDLogDebug(@"MediaListView - Photo saved to photos album...");

                        // Get photo name
                        [self getNameFromUrl:assetURL];
                    }
                }];
            }
            else {
                DDLogDebug(@"MediaListView - This is an existing image, getting name...");
                // Get photo name
                [self getNameFromUrl:[info objectForKey:@"UIImagePickerControllerReferenceURL"]];
            }
        }
    }
    @catch (NSException *exception) {
        DDLogError(@"MediaListView - Exception in picker didFinishPickingMediaWithInfo");
        DDLogError(@"MediaListView - %@", [exception description]);
    }
}

- (void)getNameFromUrl(NSURL*)url {
    @try {
        DDLogDebug(@"MediaListView - GetNameFromUrl");

        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {

            if (asset == nil) {
                DDLogError(@"MediaListView - SaveAssetData - asset is nil!");
                return;
            }

            DDLogDebug(@"MediaListView - SaveAssetData - Got asset data: %@", asset.description);
            ALAssetRepresentation *assetRep = [asset defaultRepresentation];

            NSString *fileName = [assetRep filename];
            DDLogDebug(@"MediaListView - SaveAssetData - File name = %@", fileName);
        };

        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error) {
            DDLogError(@"MediaListView - SaveAssetData - Failed to get name%@", error);
        };

        ALAssetsLibrary *library = [ALAssetsLibrary new];
        [library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
    }
    @catch (NSException *exception) {
        DDLogError(@"MediaListView - Exception in saveAssetData");
        DDLogError(@"MediaListView - %@", [exception description]);
    }
}

这篇关于无法获取用相机捕获的图像的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆