2016-05-23 54 views
-1

我正在使用以下代码来检索图像。使用nsurl访问文件名并将其转换为基址64

UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL]; 
NSString *base64String = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 
  1. 当我尝试打印路径,它打印整个文件的路径,但我怎样才能获得的文件名出NSURL
  2. 访问路径后,我希望使用路径将图像转换为base 64。我该怎么做?
+0

为什么你使用'valueForKey:'? “我希望使用路径将图像转换为64位”是什么意思? – trojanfoe

回答

2

你可以使用

NSString *theFileName = [[string lastPathComponent] stringByDeletingPathExtension]; 

的文件名,并使用下面的代码的base64谈话

- (NSString*)base64forData:(NSData*) theData 
{ 
    const uint8_t* input = (const uint8_t*)[theData bytes]; 
    NSInteger length = [theData length]; 

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/="; 

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2)/3) * 4]; 
    uint8_t* output = (uint8_t*)data.mutableBytes; 

    NSInteger i; 
    for (i=0; i < length; i += 3) { 
     NSInteger value = 0; 
     NSInteger j; 
     for (j = i; j < (i + 3); j++) { 
      value <<= 8; 

      if (j < length) { 
       value |= (0xFF & input[j]); 
      } 
     } 

     NSInteger theIndex = (i/3) * 4; 
     output[theIndex + 0] =     table[(value >> 18) & 0x3F]; 
     output[theIndex + 1] =     table[(value >> 12) & 0x3F]; 
     output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '='; 
     output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '='; 
    } 

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; 
} 
+0

它将所有图像的文件名打印为'asset' – Roger

1

你可以得到这样的名字,

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

NSString *imageName = [imagePath lastPathComponent]; 
+0

它为所有图像打印'asset.JPG' bro – Roger

1

我怀疑你'通过错误的钥匙

NSURL *url = info[UIImagePickerControllerMediaURL]; 
NSString *fileName = url.lastPathComponent; 
+0

它将所有图像的文件名打印为'asset' – Roger

+0

查看http://stackoverflow.com/questions/ 7180880/how-to-get-a-photos-original-filename-in-ios – vadian

1

试试这个,我希望它会有帮助!在文件 进口AssetsLibrary:

#import <AssetsLibrary/AssetsLibrary.h> 

而且,在

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

// get the ref url 
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]; 

而另一种方式,你可以做这样的!

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) { 
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL 
let imageName = imageURL.path!.lastPathComponent 
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String 
let localPath = documentDirectory.stringByAppendingPathComponent(imageName) 

let image = info[UIImagePickerControllerOriginalImage] as UIImage 
let data = UIImagePNGRepresentation(image) 
data.writeToFile(localPath, atomically: true) 

let imageData = NSData(contentsOfFile: localPath)! 
let photoURL = NSURL(fileURLWithPath: localPath) 
let imageWithData = UIImage(data: imageData)! 

picker.dismissViewControllerAnimated(true, completion: nil) 

}

+0

ALAssetsLibraryAssetForURLResultBlock已弃用 – Roger

相关问题