从ios中的UIImageWriteToSavedPhotosAlbum获取图像名称

时间:2013-12-23 10:53:25

标签: ios uiimagepickercontroller ios-camera

我已经搜索了如何从相机中获取已保存图像的名称,但我没有找到简单的内容。

这是我的代码:

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

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera){
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
     {
         NSLog(@"IMAGE SAVED TO PHOTO ALBUM");
         [library assetForURL:assetURL resultBlock:^(ALAsset *asset )
          {
              NSLog(@"we have our ALAsset!");
              NSLog(@"%@", assetURL);

          }
                 failureBlock:^(NSError *error )
          {
              NSLog(@"Error loading asset");
          }];
     }];
}

}

我有答案,我很乐意测试它。

提前致谢。

2 个答案:

答案 0 :(得分:1)

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *resourceURL;
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image =[[UIImage alloc] init];

image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];

NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

NSString *imageName = [imagePath lastPathComponent];

resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];


NSData *imageData;
NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:@"."].location+1 ];

if ([extensionOFImage isEqualToString:@"jpg"])
{
    imageData =  UIImagePNGRepresentation(image);
}

else
{
    imageData =    UIImageJPEGRepresentation(image, 1.0);
}

int imageSize=imageData.length/1024;
    NSLog(@"imageSize--->%d", imageSize);
if (imageName!=nil) {
   NSLog(@"imageName--->%@",imageName);
}
else
{
   NSLog(@"no image name found");
}
}

如果您使用的是ASSerts

-(void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
[self dismissViewControllerAnimated:YES completion:^{

    if (assets.count < 1) return;

    //self.pageControl.numberOfPages = assets.count;

    int index = 0;

    for (ALAsset *asset in assets) {
        //  NSString *imageName = [[asset defaultRepresentation] filename];

        UIImage *image = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
        // (@"%@", [[asset defaultRepresentation] filename]);
        NSData *imageData = UIImagePNGRepresentation(image);
        int imageSize=imageData.length/1024;
    (either)    
   NSURL *imagePath = [asset objectForKey:@"UIImagePickerControllerReferenceURL"];
    //  NSURL *imagePath = [NSURL URLWithString:[asset ob]];
         NSString *imageName = [imagePath lastPathComponent];
    (or)
    NSLog(@"%@",[[asset defaultRepresentation] filename]);
        index++;
    }

}];
}

答案 1 :(得分:0)

尝试以下代码。我在之前的一个项目中使用了它并且它为我工作:

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

  _selectedImage = info[UIImagePickerControllerEditedImage];
  self.photoView.image = _selectedImage;

  NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

  // define the block to call when we get the asset based on the url (below)
  ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
  {
     ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
     NSLog(@"[imageRep filename] : %@", [imageRep filename]);
  };

  // get the asset library and fetch the asset based on the ref url (pass in block above)
  ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
  [assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];

  [picker dismissViewControllerAnimated:YES completion:NULL];

}