2016-05-23 1 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'を印刷しています。 – 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

関連する問題