我在UIImageView中有一个图像,并希望将其保存到设备的照片中,以便最终将其保存为壁纸。虽然代码编译没有错误,但图像不能保存,我担心在使用'UIImage'与'UIImageView'或其他所有东西时我做错了。图像的名称是“Q115birdsfull~iphone.png”,我的代码到目前为止。我做错了什么???
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);
}
` 提前谢谢!
答案 0 :(得分:3)
您尝试保存的是UIImage
作为财产和ivar保存的内容。我不在您的代码中看到的是您实际将该图像设置为任何内容的位置。这可能是你缺少的一步。
尝试这样做:
- (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?");
}
}