Bla*_*ade 1 iphone nsuserdefaults nscoding uiimagepickercontroller ios
当用户第一次启动应用程序时,他会弹出一个并且必须保存图像.它适用于模拟器和4S.但是当我用我的3G开始它时,一旦我选择了一张照片就会给我一个SIGABRT错误.我假设它是因为图片的大小过于挑选并因此声称所有的公羊 - 但这是相当奇怪的,因为我把它做得更小.这是代码:
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"bild"] == nil) {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
double compressionRatio=1;
NSData *imageData=UIImageJPEGRepresentation([info objectForKey:@"bild"],compressionRatio);
while ([imageData length]>5000) {
compressionRatio=compressionRatio*0.20;
imageData=UIImageJPEGRepresentation([info objectForKey:@"bild"],compressionRatio);
}
UIImage *yourUIImage;
yourUIImage = [info objectForKey:UIImagePickerControllerOriginalImage];
imageData = [NSKeyedArchiver archivedDataWithRootObject:yourUIImage];
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:@"bild"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self dismissModalViewControllerAnimated:YES];
}
Run Code Online (Sandbox Code Playgroud)
我在此行得到SIGABRT错误imageData = [NSKeyedArchiver archivedDataWithRootObject:yourUIImage];
我应该使用其他方法来调整图像大小吗?或者我应该将其保存为本地文件并在应用程序启动时检索它?(如果它是可能的)
您正在使用NSCoding将UIImage存档为NSData对象.
在iOS5之前,UIImage没有实现NSCoding的方法.你不能archivedDataWithRootObject:在iOS5之前使用UIImages.
这就是你在iPhone 3G上获得例外的原因.
这只是一个非常有根据的猜测,因为我无法通过文档或设备上的测试来确认,但是有很多问题和论坛帖子询问如何在UIImage上实现NSCoding.
并且根本不会调用整个代码块,因为[info objectForKey:@"bild"]它将返回nil.信息字典不包含密钥bild的对象.文档UIImagePickerControllerDelegate_Protocol包含有效密钥列表
NSData *imageData=UIImageJPEGRepresentation([info objectForKey:@"bild"],compressionRatio);
while ([imageData length]>5000) {
compressionRatio=compressionRatio*0.20;
imageData=UIImageJPEGRepresentation([info objectForKey:@"bild"],compressionRatio);
}
Run Code Online (Sandbox Code Playgroud)
你可能想要使用这样的东西:
NSData *data;
if ([[UIImage class] conformsToProtocol:@protocol(NSCoding)]) {
// >= iOS5
data = [NSKeyedArchiver archivedDataWithRootObject:image];
// save so you know it's an archived UIImage object
}
else {
// < iOS5
data = /* your UIImageJPEGRepresentation method but this time with the key UIImagePickerControllerOriginalImage instead of "bild" */
// save so you know it's raw JPEG data
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
500 次 |
| 最近记录: |