在objective-c中的UIImage中图像名称为空
Posted
技术标签:
【中文标题】在objective-c中的UIImage中图像名称为空【英文标题】:image name is null in UIImage in objective-c 【发布时间】:2016-06-30 06:27:38 【问题描述】:-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image =[[UIImage alloc] init];
image =[info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
imageName = [imagePath lastPathComponent];
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");
//commented ashok
NSURL *resourceURL = [info objectForKey:UIImagePickerControllerMediaURL];
resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *assetLibrary = [ALAssetsLibrary new];
[assetLibrary assetForURL:resourceURL
resultBlock:^(ALAsset *asset)
// get data
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CGImageRef cgImg = [assetRep fullResolutionImage];
filename = [assetRep filename];
NSLog(@"file name is:%@", filename);
failureBlock:^(NSError *error)
NSLog(@"%@", error);
];
-(void)send message
NSLog(@"image name is:%@",filename);
//image name is: IMG_0004.JPG
senderImgName=[UIImage imageNamed:filename];
NSLog(@"sender image name is :%@",senderImgName);
//sender image name is: null
【问题讨论】:
@BhavinRamani - 文件名为:IMG_0005.JPG @BhavinRamani 文件名是字符串变量.. 在具有图像名称的文件名中 确认您的图片资源已添加到您的目标中。 @Desdenova 实际上我正在从照片库中挑选图像。所以应该在这里传递图像名称 这会对你有所帮助 - ***.com/questions/6674549/… 【参考方案1】:当你使用UIImagePickerController
时,你需要将UIImage
对象赋予imageView
而不是名称,像这样更改你的代码
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image
希望这会对你有所帮助。
【讨论】:
感谢您的回复.. 但我正在使用objective-c .. 你会发布objective-c 代码' @satya 使用我的答案,因为 singare 使用了已弃用的方法。 我正在更新我的查询.. 请检查一次 你想做什么?仅添加代码对我们没有帮助。请指定您在从ImagePicker
选择图像后要做什么。
从图像选择器中选择图像后,我想将图像发送到服务器。【参考方案2】:
试试这个
//This will show the picker
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
[self presentViewController:picker animated:YES completion:nil];
//Deprecated In ios6[self presentModalViewController:picker animated:YES];
[picker release];
//这个委托方法会给你选择的图像。
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
imgProfilePic.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissModalViewControllerAnimated:YES];
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
[picker dismissModalViewControllerAnimated:YES];
【讨论】:
此方法已弃用。 @Signare 我正在更新我的查询.. 请您检查一次 @Signare 请给我指导..我需要你的帮助【参考方案3】:检查您是否将图像添加到项目中。 如果项目中存在图像,请检查它是否包含在属性中使用目标。
【讨论】:
【参考方案4】:试试这个
ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CGImageRef cgImg = [assetRep fullResolutionImage];
UIImage* image = [UIImage imageWithCGImage:cgImg];
【讨论】:
感谢您的回复.. 但我遇到了错误...您能给我一些其他的 ide 你能不能来聊天室我想和你谈谈以上是关于在objective-c中的UIImage中图像名称为空的主要内容,如果未能解决你的问题,请参考以下文章
如何使 UIImage 在 Objective-c 的 Xcode 中工作