【问题标题】:iOS attempting to save image to camera roll but not succeedingiOS 尝试将图像保存到相机胶卷但未成功
【发布时间】:2012-06-25 09:05:54
【问题描述】:

我在 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);
}

` 提前谢谢!

【问题讨论】:

  • 首先,这与 Xcode 无关,而是与 iOS 相关,因此我将为您删除所有提及 Xcode 的内容。 :-)
  • 我明白你的意思,迈克尔。谢谢!
  • 也许这可以帮助你stackoverflow.com/questions/3017681/…
  • 你好。 Q115birdsfull 实际上是否包含图像?我没有看到你给它任何数据。
  • @qegal 再次感谢!也许我错过了一步。我正在使用情节提要并将图像视图拖到视图控制器上,然后从我的支持文件+上面的代码中分配我的 .png 图像。

标签: iphone objective-c ios uiimage


【解决方案1】:

您尝试保存的是作为属性和 ivar 保存的 UIImage。我没有在您的代码中看到的是您实际将该图像设置为任何内容的位置。这可能是您缺少的步骤。

尝试这样做:

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

【讨论】:

  • 你太棒了!它完全奏效了。我被标记在图像名称前面的“@”处,我将在上面的代码中添加它。非常感谢你!
  • 顺便说一下,我希望你不要错过我关于 Objective C 命名风格的评论:变量和方法名应该总是以小写字母开头,类名应该以小写字母开头用大写字母。希望这会有所帮助!
  • 我确实注意到了......也谢谢你......我会做出这个改变。非常感激。所以我无法编辑上面的代码,因为更改需要 6 个字符或更多,所以我将尝试在下面发布。再次感谢你!!!
  • 所以我似乎无法再发布 7 个小时的编辑答案。那我明天再说。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-24
  • 1970-01-01
  • 2022-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多