Reputation: 1407
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!
Upvotes: 0
Views: 2131
Reputation: 89569
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?");
}
}
Upvotes: 3