0

I have an image in an UIImageView and want to save it to the device's photos so that it can be ultimately saved as a wallpaper. Although the code compiles without an error the image does not save and I fear that I am doing something wrong when it comes to using 'UIImage' vs 'UIImageView' or something else all together. The name of the image is "Q115birdsfull~iphone.png" and my code thus far is below. What am I doing wrong???

Q115birdsViewController.h

#import <UIKit/UIKit.h>

@interface Q115birdsViewController : UIViewController 
{
    UIImage *Q115birdsfull;
}

@property (nonatomic, strong) UIImage *Q115birdsfull;

- (IBAction)onClickSavePhoto:(id)sender;

@end

Q115birdsViewController.m

#import "Q115birdsViewController.h"

@interface Q115birdsViewController ()
@end

@implementation Q115birdsViewController

@synthesize Q115birdsfull;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (IBAction)onClickSavePhoto:(id)sender{
    UIImageWriteToSavedPhotosAlbum(Q115birdsfull, nil, nil, nil);
}

` Thank you in advance!

pasawaya
  • 11,515
  • 7
  • 53
  • 92
user1470914
  • 1,367
  • 2
  • 9
  • 10
  • first off, this has nothing to do with Xcode but instead with iOS, so I'm removing all mention of Xcode for you. :-) – Michael Dautermann Jun 23 '12 at 04:23
  • I see what you mean Michael. Thx! – user1470914 Jun 23 '12 at 04:26
  • maybe this can help you http://stackoverflow.com/questions/3017681/uiimagewritetosavedphotosalbum-working-sometimes – ewiinnnnn Jun 23 '12 at 04:28
  • Hey again. Does `Q115birdsfull` actually contain an image? I don't see you giving it any data at all. – pasawaya Jun 23 '12 at 04:31
  • @qegal Thanks again so much! Perhaps I have missed a step. I am using storyboard and draged an Image View onto a View Controller and then assigned my .png image from my Supporting Files + the code above of course. – user1470914 Jun 23 '12 at 04:37
  • @ewiinnnn Thx! I hadn't noticed this link in my search but it didn't quite help me. I fear that I am making a simple error. – user1470914 Jun 23 '12 at 04:41
  • possible duplicate of [iOS save image to camera roll](http://stackoverflow.com/questions/11131050/ios-save-image-to-camera-roll) – John Conde Jun 24 '12 at 05:21

1 Answers1

3

What you're attempting to save is a UIImage kept as a property and an ivar. What I don't see in your code is where you actually set that image to anything. That may be the step you are missing.

Try doing this:

- (IBAction)onClickSavePhoto:(id)sender{

    if(Q115birdsfull == NULL)
    {
        NSLog( @"there is no Q115birdsfull image set");
        Q115birdsfull = [UIImage imageNamed: @"Q115birdsfull"];

        // if it's STILL null, we'll try a much more specific name
        if(Q115birdsfull == NULL)
        {
            Q115birdsfull = [UIImage imageNamed: @"Q115birdsfull~iphone"];
        }
    }

    if(Q115birdsfull){
        // by the way, variable names should *always* start with lower case letters
        UIImageWriteToSavedPhotosAlbum(Q115birdsfull, nil, nil, nil);
    }
    else {
        NSLog( @"never found the Q115birdsfull png file... is it really being copied into your built app?");
    }
}
pckill
  • 3,709
  • 36
  • 48
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • You are amazing! It totally worked. I was flagged to at an "@" in front of the image name which I will add to your code above. Thanks you sooooooooooo much! – user1470914 Jun 23 '12 at 05:12
  • 1
    by the way, I hope you didn't miss my comment about Objective C naming styles: variables and method names should *always* start with lower case letters and class names should start with capital letters. Hope this helps! – Michael Dautermann Jun 23 '12 at 05:13
  • I did notice that...thank you as well...I will make this change. Much appreciated. So I couldn't edit the code above because the change needed to be 6 characters or more so I will try to post below. Thank you again so much!!! – user1470914 Jun 23 '12 at 05:16
  • So I can't post the answer with the edit for another 7 hours it seems. I will tomorrow then. – user1470914 Jun 23 '12 at 05:18