我正在尝试将图像从我的iPhone / iPod touch上传到我的在线存储库,我已成功从相册中选取图像,但我遇到一个问题,我想知道图像的名称,如image1.jpg或类似的东西。我怎么知道所选图像的名称。
答案 0 :(得分:6)
您可以使用(UIImage*)[info valueForKey:UIImagePickerOriginalImage]
并导出实际的源文件(包括格式,名称和所有内容),而不是使用通常的图像选择器方法AssetsLibrary.framework
来为您提供所选图像作为UIImage的实例。元数据)。这也具有保留原始文件格式(png或jpg)的优点。
#import <AssetsLibrary/AssetsLibrary.h>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self dismissPicker];
// try to get media resource (in case of a video)
NSURL *resourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
if(resourceURL) {
// it's a video: handle import
[self doSomethingWith:resourceURL];
} else {
// it's a photo
resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *assetLibrary = [ALAssetsLibrary new];
[assetLibrary assetForURL:resourceURL
resultBlock:^(ALAsset *asset) {
// get data
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CGImageRef cgImg = [assetRep fullResolutionImage];
NSString *filename = [assetRep filename];
UIImage *img = [UIImage imageWithCGImage:cgImg];
NSData *data = UIImagePNGRepresentation(img);
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSURL *tempFileURL = [NSURL fileURLWithPath:[cacheDir stringByAppendingPathComponent:filename]];
BOOL result = [data writeToFile:tempFileURL.path atomically:YES];
if(result) {
// handle import
[self doSomethingWith:resourceURL];
// remove temp file
result = [[NSFileManager defaultManager] removeItemAtURL:tempFileURL error:nil];
if(!result) { NSLog(@"Error removing temp file %@", tempFileURL); }
}
}
failureBlock:^(NSError *error) {
NSLog(@"%@", error);
}];
return;
}
}
答案 1 :(得分:4)
我想知道确切的图像名称不会成为问题,而是为拾取的图像获取唯一名称将解决您的目的,以便您可以在服务器上传图像并通过其名称跟踪它。可能这可以帮到你
NSMutableString *imageName = [[[NSMutableString alloc] initWithCapacity:0] autorelease];
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
[imageName appendString:NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID))];
CFRelease(theUUID);
}
[imageName appendString:@".png"];
从Picker中选择图像后,您可以生成唯一名称并将其指定给Picked图像。 干杯