【发布时间】:2012-02-28 13:34:51
【问题描述】:
我有一个名为“保存到相册”的保存按钮。按钮顶部是小的 UIImageView,用户可以单击下面的按钮将照片的实际大小保存到相册。我的问题是当我点击保存按钮时,它显示一个错误:无法将图像保存到相册。
我认为我做得对,但我的经验太有限了。
H 文件:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
IBOutlet UIButton *btnReturn;
IBOutlet UIButton *savePhoto;
IBOutlet UIImage *saveImage;
}
@property (nonatomic, retain) IBOutlet UIButton *btnReturn;
@property (nonatomic, retain) IBOutlet UIButton *savePhoto;
@property (nonatomic, retain) IBOutlet UIImage *saveImage;
-(IBAction) saveToPhotoAlbum;
@end
下面是我的代码:
-(IBAction) saveToPhotoAlbum{
NSString *saveMyPhoto=[NSHomeDirectory() stringByAppendingPathComponent:@"sample.png"];
UIImage *saved=[UIImage imageWithContentsOfFile:saveMyPhoto];
NSData *imageData = [NSData dataWithContentsOfFile:(NSString *)UIImagePNGRepresentation(saved)];
[imageData writeToFile:(NSString *)saved atomically:YES ];
UIImageWriteToSavedPhotosAlbum(saved, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
if (error)
alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
else
alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"Image saved to Photo Album."
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
【问题讨论】:
-
实际错误是什么(即在您的图像中添加
NSLog(@"%@", error);:didFinishSavingWithError:contextInfo: 方法。 -
图片无法保存到相册。程序可以成功运行。只是它无法保存。我的代码有没有可能不正确?
-
不,这不是错误,这只是您在警报中输入的信息。当您在图像中添加行
NSLog(@"%@", error);时会发生什么情况:didFinishSavingWithError:contextInfo: 方法 - 应该有一个错误输出到控制台。我们需要知道那是什么。 -
好的。这是错误。错误域=ALAssetsLibraryErrorDomain 代码=-3304 “未能为保存的照片编码图像。” UserInfo=0x7857630 {NSUnderlyingError=0x7851f20 "无法为保存的照片编码图像。", NSLocalizedDescription=无法为保存的照片编码图像。}
-
我该怎么办?我只是想成功保存图像。
标签: objective-c xcode4 ios5 ios4 uiimageview