-(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
}
Asked
Active
Viewed 705 times
1
-
@BhavinRamani - file name is: IMG_0005.JPG – Rince Thomas Jun 30 '16 at 06:30
-
@BhavinRamani filename is string variable.. in filename having image name – satya Jun 30 '16 at 06:30
-
Confirm that your image resources are added to your target. – Desdenova Jun 30 '16 at 06:30
-
@Desdenova actually i am picking image from photo gallery. so the image name should be passed here – satya Jun 30 '16 at 06:31
-
@satya you are using imagePickerController ? – Rince Thomas Jun 30 '16 at 06:33
-
That changes things. `[UIImage imageNamed:filename]` will look for the image in the bundle. So it's perfectly normal if it comes as nil. You need to use `+ imageWithContentsOfFile:` with the image path. – Desdenova Jun 30 '16 at 06:34
-
@Signare yes i am using imagepickercontroller – satya Jun 30 '16 at 06:36
-
@satya Check my answer – Nirav D Jun 30 '16 at 06:36
-
1this will help you - http://stackoverflow.com/questions/6674549/display-images-from-gallery-in-iphone – Rince Thomas Jun 30 '16 at 06:36
-
@Desdenova thanks for your response.. will you give me any idea/// – satya Jun 30 '16 at 06:36
-
@satya Object image is giving you data ? – Rince Thomas Jun 30 '16 at 06:52
-
@Signare no bro.. data getting nil – satya Jun 30 '16 at 06:54
-
@satya what about cgImg ? – Rince Thomas Jun 30 '16 at 06:56
-
if you are getting value in `cgImg`, then use `UIImage* image = [UIImage imageWithCGImage:cgImg];` Refer @Anessence answer – Rince Thomas Jun 30 '16 at 06:58
4 Answers
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
-
thanks for your response.. but i am using objective-c.. will you post objective-c code' – satya Jun 30 '16 at 06:37
-
-
-
What do you want to do? Adding only code does not help us. Please specify what do you want to do after picking image from `ImagePicker`. – Nirav D Jun 30 '16 at 07:05
-
after picking the image from image picker i want to send the image to server. – satya Jun 30 '16 at 07:17
-
Then you just need to convert your image in to data and encode it and send it the server. – Nirav D Jun 30 '16 at 07:21
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116048/discussion-between-satya-and-nirav). – satya Jun 30 '16 at 07:24
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
-
thanks for your response.. but i got error...will you give me some other ide – satya Jun 30 '16 at 07:37
-