UIImage returns nil (xcode5) - ios

My UIImage is nil if I try to test this block of code:
- (IBAction)sliderBrightness:(id)sender {
UIImage *inputImage = imgView.image;
GPUImageBrightnessFilter *stillImageFilter = [[GPUImageBrightnessFilter alloc] init];
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:inputImage];
[stillImageSource addTarget:stillImageFilter];
[stillImageFilter useNextFrameForImageCapture];
[stillImageSource processImage];
UIImage *currentFilteredVideoFrame = [stillImageFilter imageFromCurrentFramebuffer];
[self->imgView setImage:currentFilteredVideoFrame];
}
UPDATE:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
imgView.image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
toolbar.hidden = NO;
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
}
if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
toolbar.hidden = NO;
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
}
if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
toolbar.hidden = NO;
UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:#"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];
}
else{
toolbar.hidden = NO;
}
[self dismissViewControllerAnimated:YES completion:nil];
}
Debugger (inputImage is nil):
I hope someone can help me.
Thanks in advance for your help.
I'm using xcode 5.1.1 (iOS Simulator: iPhone 4-inch; 64-Bit; iOS7.1)

If (as you say in comments) the problem is that inputImage is nil, the problem is external to the code you've posted. Either imgView is itself nil, or it points to a UIImageView instance that hasn't yet had its image set. Put a breakpoint on the line:
UIImage *inputImage = imgView.image;
and inspect imgView to determine which of those possibilities is the problem.

Related

Setting Image from gallery in iOS

I select an image from gallery and display on UIImageView but when I click back button and again open push to the image view controller that image is empty.
What should I do to remain same image on UIImageView after logout also?
I have used this code:
- (void)viewDidLoad {
NSString *myGrrabedImage1=#"myGrrabedImage1.png";
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1=[path objectAtIndex:0];
NSString *fullPathToFile1=[documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
NSData *data=[NSData dataWithContentsOfFile:fullPathToFile1];
[[self teacherImg]setImage:[UIImage imageWithData:data]];
[data writeToFile:fullPathToFile1 atomically:YES];
}
- (IBAction)selectImg:(id)sender {
pickerController = [[UIImagePickerController alloc]init];
pickerController.delegate = self;
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage];
teacherImg.image = img;
[self presentViewController:pickerController animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
NSData *data=UIImagePNGRepresentation(teacherImg.image);
NSString *myGrrabedImage1=#"myGrrabedImage1.png";
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1=[path objectAtIndex:0];
NSString *fullPathToFile=[documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
[data writeToFile:fullPathToFile atomically:YES];
imagePicker.allowsEditing = YES;
imagePicker.delegate = self;
[[self teacherImg]setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}
In viewDidLoad() method you need to load image from doc. dir.
NSString *myGrrabedImage1 = #"myGrrabedImage1.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1 = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
Method 1:
NSData *imgData = [NSData dataWithContentsOfFile: fullPathToFile];
UIImage *thumbNail = [[UIImage alloc] initWithData:imgData];
Method 2:
UIIMage *image = [UIImage imageWithContentsOfFile: fullPathToFile];
[imgView setImage: image];
You should read the image data and set the ImageView image in the viewWillAppear method.
viewDidLoad is called only once, after the view has been loaded from the xib/storyboard
Try this way;
- (void)viewDidLoad {
NSString *myGrrabedImage1 = #"myGrrabedImage1.png";
NSArray *path =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1 = [path objectAtIndex:0];
NSString *pathFile = [documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
UIIMage *image = [UIImage imageWithContentsOfFile: pathFile];
[teacherImg setImage: image];
}
- (IBAction)selectImg:(id)sender {
pickerController = [[UIImagePickerController alloc]init];
pickerController.delegate = self;
[self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:pickerController animated:YES completion:nil];
}
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
UIImage * img = [info valueForKey:UIImagePickerControllerEditedImage];
teacherImg.image = img;
NSData *data=UIImagePNGRepresentation(teacherImg.image);
NSString *myGrrabedImage1=#"myGrrabedImage1.png";
NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory1=[path objectAtIndex:0];
NSString *fullPathToFile=[documentDirectory1 stringByAppendingPathComponent:myGrrabedImage1];
[data writeToFile:fullPathToFile atomically:YES];
[[self teacherImg]setImage:image];
[self dismissViewControllerAnimated:YES completion:nil];
}

SDWebImage ,setImageWithUrl can't show the image on the imageView

- (void)xuanquButtonClicked:(JohnButtonLong*)button
{
_picker = [[UIImagePickerController alloc] init];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
JLActionManager * manager = [JLActionManager sharedInstance];
[manager showAndHideHUDWithTitle:nil detailText:#"亲,您的相册无法打开" inView:self.view];
return;
}
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_picker.allowsEditing = YES;
_picker.delegate = self;
[self presentViewController:_picker animated:YES completion:^{
}];
}
#pragma mark - UIIMagePickerController delegate Methods
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:^{
}];
// 如果没有选取 使用默认头像
_strHeadPic = #"";
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
}];
// 选取了某个头像
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
_img_touxiang.image = image;
//压缩图像
image = [self OriginImage:image scaleToSize:CGSizeMake(160, 160)];
NSData * img_data = UIImageJPEGRepresentation(image, 0.5);
//进行base64编码
NSData * data = [GTMBase64 encodeData:img_data];
_strHeadPic = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
// 压缩成指定大小
-(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size{
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();// 是当前的context出对战
// 返回新的改变大小后的图片
return scaledImage;
}
UnitMyInfo * myInfo = GetMyInfo;
[_img_touxiang setImageWithURL:[NSURL URLWithString:BaseImgURL(myInfo.umi_URL)] placeholderImage:[UIImage imageNamed:#"default_headImg.jpg"]];
// First You have to get Url from Image then set url On SDWebImages.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
NSURL *imagePath = [editingInfo objectForKey:#"UIImagePickerControllerReferenceURL"];
NSString *imageName = [imagePath lastPathComponent];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"localFilePath.%#",localFilePath);
}
//Set Image On SDWebView.
[imageView setImageWithURL:[NSURL URLWithString:localFilePath] placeholderImage:[UIImage imageNamed:#"default_headImg.jpg"]];

UIImage stretching for unknown reason

EDIT: This only happens in the phone. It works fine in the simulator.
I am having a weird issue that I can't seem to rectify.
In the app I am building you can add and replace a profile photo and here is what is happening.
When you either select an image from your library or take a new one with the camera, you are returned to your profile view and you can see the new image.
Here it is with a temp image.
But when I go back to the main nav and then return to the profile page, the image is wider than it should be.
I am at a real loss here. The code to call each image is virtually identical.
// Code after initial image selection
UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:savedImagePath atomically:NO];
profileImage.image = image;
//Code after returning to the page.
UIImage *image = [[UIImage alloc] initWithContentsOfFile:directoryWithProfilePicName];
profileImage.image = image;
Any thoughts would be great.
UPDATE 1:
// Creating directoryWithProfilePicName.
NSString *localDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *profilePicName = [NSString stringWithFormat:#"/%#", profilePic];
NSString *directoryWithProfilePicName = [localDirectory stringByAppendingString:profilePicName];
// Creating the image view.
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
if (IS_IPHONE5) {
profileImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 335, 300)];
} else {
profileImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, 335, 210)];
}
UPDATE 2:
- (void)takeNewPhotoFromCamera {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
-(void)choosePhotoFromExistingImages {
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
NSString *uuidString = [[NSUUID UUID] UUIDString];
NSString *imageName = [NSString stringWithFormat:#"%#.jpg", uuidString];
UIImage *image = [info valueForKey: UIImagePickerControllerOriginalImage];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)];
NSLog(#"Pic Name: %#", imageName);
NSLog(#"Image Size: %f x %f", image.size.width, image.size.height);
NSLog(#"picture Taken.");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:imageName];
[imageData writeToFile:savedImagePath atomically:NO];
[UIView saveNewPhoto:imageName forPetID:pIDint];
profileImage.image = image;
profileImage.contentMode = UIViewContentModeScaleAspectFit;
}
I'm guessing your problem is caused by your UIImageView's content mode.
theImageView.contentMode = UIViewContentModeScaleAspectFit;
Make sure you are using ScaleAspectFit, otherwise your UIImageView will default to UIViewContentModeScaleToFill.

Saving image from camera to documents directory

I have the following code which launches the camera app and the user can select "use photo". However nothing happens.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePickerController animated:YES];
}
How I can I get an UIimage from this and save it to the photo album. Ideally I'd like to save this to the documents directory and know how to do that part. Thanks!
-(void) imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
UIImage* originalImage = nil;
originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
if(originalImage==nil)
{
originalImage = [info objectForKey:UIImagePickerControllerCropRect];
}
[addImageOutlet setImage:originalImage forState:UIControlStateNormal];
[self saveBackgroundImageInDocumentDirectory:originalImage];
[self dismissViewControllerAnimated:YES completion:nil];
}
Use this code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData * imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
[imageData writeToFile:savedImagePath atomically:NO];
[picker dismissViewControllerAnimated:YES completion:nil];
}
I think it will help you,
//you have to set the delegate which clicking the button of camera
imagePickerController.delegate = self;
//This Delegate method will call
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage* selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSString *path = [[self pathToPatientPhotoFolder] stringByAppendingPathComponent:[NSString stringWithFormat:#"imageName.png"]];
NSError * error11 = nil;
[post.getData writeToFile:path options:NSDataWritingAtomic error:&error11];
}
//This method is useful for the get the Documentry path
- (NSString *)pathToPatientPhotoFolder {
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES) lastObject];
NSString *myalbumpath = [documentsDirectory stringByAppendingPathComponent:#"MYAlbum"];
// Create the folder if necessary
BOOL isDir = NO;
NSFileManager *fileManager = [[NSFileManager alloc] init];
if (![fileManager fileExistsAtPath:myalbumpath
isDirectory:&isDir] && isDir == NO) {
[fileManager createDirectoryAtPath:myalbumpath
withIntermediateDirectories:NO
attributes:nil
error:nil];
}
return myalbumpath;
}
Enjoy the coding
addImageOutlet for what type of object

array of images in document directory

I have select multiple images from iPhone through ELC controller. The images are stored in array now I want to store this array of images in document directory so please help me someone..
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
if ([self respondsToSelector:#selector(dismissViewControllerAnimated:completion:)]){
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[self dismissModalViewControllerAnimated:YES];
}
for (UIView *v in [_scrollView subviews]) {
[v removeFromSuperview];
}
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
}
self.chosenImages = images;
NSLog(#"values=%#",_chosenImages);
[_scrollView setPagingEnabled:YES];
[_scrollView setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
}
For each of the images in the images array, write to the file file one by one.
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSInteger anIndex = 0;
for (UIImage *anImage in images) {
NSString *anImageName = [NSString stringWithFormat:#"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:#"%#/%#", aDocumentsDirectory, anImageName];
NSData *anImageData = UIImagePNGRepresentation(anImage);
[anImageData writeToFile:anImagePath atomically:YES];
}
2.. When you extract the Original Image, save image to file right there.
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSInteger anIndex = 0;
for(NSDictionary *dict in info) {
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[_scrollView addSubview:imageview];
[imageview release];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
// Save Image
NSString *anImageName = [NSString stringWithFormat:#"%d.png", anIndex++];
NSString *anImagePath = [NSString stringWithFormat:#"%#/%#", aDocumentsDirectory, anImageName];
NSData *anImageData = UIImagePNGRepresentation(image);
[anImageData writeToFile:anImagePath atomically:YES];
}

Resources