1
-(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
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
satya
  • 241
  • 3
  • 11

4 Answers4

2

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.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
2

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];
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
1

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.

John Snow
  • 19
  • 2
1

Try this

ALAssetRepresentation *assetRep = [asset defaultRepresentation];
CGImageRef cgImg = [assetRep fullResolutionImage];
UIImage* image = [UIImage imageWithCGImage:cgImg];
Anessence
  • 589
  • 5
  • 20