satya satya - 1 year ago 151
iOS Question

image name is null in UIImage in objective-c

-(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);
imageData = UIImageJPEGRepresentation(image, 1.0);

int imageSize=imageData.length/1024;
NSLog(@"imageSize--->%d", imageSize);
if (imageName!=nil) {
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

Answer Source

You need to give the UIImage object to the imageView not the name when you are using UIImagePickerController, Change your code like this

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
     self.imageView.image = image

Hope this will help you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download