Question

I'll be creating an UIImage but if I debug the method it returns nil. I hope someone know what I have to do.

My code:

returns nil:

UIImage *image1 = imgView.image;

Whole block of code:

UIImage *image1 = imgView.image;
NSString *cachedFolderPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString *cachedImagePath = [cachedFolderPath stringByAppendingPathComponent:@"image1.png"];
[UIImagePNGRepresentation(image1) writeToFile:cachedImagePath atomically:YES];

Update:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    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;

    }

    imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

    [self dismissViewControllerAnimated:YES completion:nil];
}
Was it helpful?

Solution 2

I think you are trying to use the imgView.image property even before you set it.

imageView.image is being set with this line

imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

Move that line to beginning of the method. like this

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

}

OTHER TIPS

The first time you are setting imgView.image to a value is near the end of imagePickerController:didFinishPickingMediaWithInfo:. Therefore, the first time this method is called, imgView.image is going to be nil until it is set at the end with:

imgView.image = [info objectForKey:@"UIImagePickerControllerEditedImage"];

Sorry, gonna skip reading your code and just post what works for me.

- (UIImage *) getImage{

    NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:@"somefile.png"  ofType:nil];
    if ([[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName])
    {
        return [UIImage imageNamed:@"somefile.png"];
    }
    else{
        NSString *t = [self pathToImageInCache:@"somefile.png"];

        if ([[NSFileManager defaultManager] fileExistsAtPath:t]){
            NSLog(@"fiel exists");
            return [[UIImage alloc]initWithContentsOfFile:t];
        }
        else{
            NSLog(@"fiel does not exists");
        }
    }

    return nil;
}

- (NSString *) pathToImageInCache: (NSString *) imageName{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@""];

    NSString *URLImg = [documentsDirectory stringByAppendingPathComponent:imageName];
    return URLImg;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top