【问题标题】:Accessing filename out of nsurl and converting the same to base 64从 nsurl 访问文件名并将其转换为 base 64
【发布时间】:2016-05-23 13:35:18
【问题描述】:

我正在使用以下代码来检索图像。

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。我该怎么做?

【问题讨论】:

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

标签: ios objective-c


【解决方案1】:

您可以使用

获取文件名
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[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    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];
}

【讨论】:

  • 它将asset打印为所有图像的文件名
【解决方案2】:

你可以得到类似的名字,

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

 NSString *imageName = [imagePath lastPathComponent];

【讨论】:

  • 它正在为所有图像打印asset.JPG 兄弟
【解决方案3】:

我怀疑你传错了钥匙

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

【讨论】:

【解决方案4】:

试试这个我希望它会有所帮助! 在您的文件中导入 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)

}

【讨论】:

  • ALAssetsLibraryAssetForURLResultBlock 已弃用
猜你喜欢
  • 2020-10-16
  • 2018-09-30
  • 2020-08-19
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
相关资源
最近更新 更多