简体   繁体   中英

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

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.

Try this

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

//This delegate method will give you the selected image.

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    imgProfilePic.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    [picker dismissModalViewControllerAnimated:YES];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

Check if you added your image to the project. If image exist in the project, check if it is included for using target in properties.

Try this

ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CGImageRef cgImg = [assetRep fullResolutionImage];
UIImage* image = [UIImage imageWithCGImage:cgImg];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM