2012-06-23 42 views
0

我在UIImageView中有一个图像,并希望将其保存到设备的照片,以便它最终可以保存为壁纸。虽然代码编译没有错误,但图像并没有保存,我担心在使用'UIImage'和'UIImageView'或其他所有东西时,我会做错事。图像的名称是“Q115birdsfull〜iphone.png”,我的代码到目前为止在下面。我究竟做错了什么???iOS试图将图像保存到相机胶卷但未成功

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

首先,这有什么好做的Xcode而是与iOS的,所以我删除的Xcode都不在话下为您服务。 :-) –

+0

我明白你的意思是迈克尔。谢谢! – user1470914

+0

也许这可以帮助你http://stackoverflow.com/questions/3017681/uiimagewritetosavedphotosalbum-working-sometimes – ewiinnnnn

回答

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?"); 
    } 
} 
+0

你真了不起!它完全奏效。我被标记为在图像名称前面的“@”,我将添加到上面的代码中。谢谢你sooooooooooo了! – user1470914

+1

顺便说一句,我希望你不要错过我关于Objective C命名风格的评论:变量和方法名应该总是以小写字母开头,类名应该以大写字母开头。希望这可以帮助! –

+0

我确实注意到......也很感谢你......我会做出这样的改变。非常感激。所以我无法编辑上面的代码,因为更改需要6个字符或更多,所以我会尝试在下面发布。再次感谢你! – user1470914

相关问题