我正在构建一个应用程序,您可以在其中制作图片或从相机胶卷中选择图像并将其保存在应用程序内以显示在tableview和detailview中。 问题是,当我从应用程序内部制作图片并保存它时,tableview和detailview变得非常慢,就像应用程序冻结一样。 我也得到这个错误“:CGAffineTransformInvert:奇异矩阵。”当我从相机中加载未从我的应用程序内部拍摄的图片时,没有问题,应用程序运行非常流畅。
这是我打开相机和相机胶卷时的代码:
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == actionSheet.cancelButtonIndex) {
return;
}
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = NO;
switch (buttonIndex) {
case 0:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
break;
case 1:
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
break;
}
[self presentModalViewController:picker animated:YES];
}
这是我将其保存到相机胶卷并将图像放入imageView:
的地方-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
UIImage *mijnImage;
if (CFStringCompare((__bridge_retained CFStringRef)mediaType, kUTTypeImage, 0)==kCFCompareEqualTo) {
mijnImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
mijnPlaatje_.image = mijnImage;
UIImageWriteToSavedPhotosAlbum(mijnImage, nil, nil, nil);
}
[picker dismissModalViewControllerAnimated:YES];
}
在这里,我将图片保存在我的应用程序中:
-(IBAction)save:(id)sender{
drankjes.plaatje = [mijnPlaatje_ image];
[self dismissModalViewControllerAnimated:YES];
}
我做错了什么?
答案 0 :(得分:1)
您需要使用:
UIImageWriteToSavedPhotosAlbum(image, self, @selector(video:didFinishSavingWithError:contextInfo), nil)
并实施:
- (void)image:(UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
NSLog(@"Saving image");
if(error != nil) {
NSLog(@"Error saving: %@",[error localizedDescription]);
}
}
正如Jensen2K所说,即使没有错误,也会调用didFinishSavingWithError
,因此您可以使用它来判断保存何时结束。
答案 1 :(得分:0)
尝试添加此委托方法以查看错误:
- (void)image:(UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo {
NSLog(@"Saving image");
if(error != nil) {
NSLog(@"Error saving: %@",[error localizedDescription]);
}
}
答案 2 :(得分:0)
要保存,请在您的应用程序中尝试使用didFinishLaunchingWithOptions方法:
NSString *path = [[NSBundle mainBundle]pathForResource:@"" ofType:@""];
UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo), nil);
答案 3 :(得分:0)
首先尝试将图像从相机中单击保存为NSData,然后以NSData格式访问图像,将其转换为UIImage并显示在imageView上。
例如,您可以使用文档目录创建一个plist,您可以在该方法中将图像另存为NSData -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
}
当您使用的方法保存图片时 - - (IBAction为)保存:(ID)发送{ }
您可以从Document目录中的plist访问相同的图像。将NSData转换为UIImage并将其显示在imageView上。我确定没有错误可以存活。
此错误“:CGAffineTransformInvert:奇异矩阵。”由于图像的图像方向属性而发生。当保存图像时,NSData存储图像属性。并且您得到的图像没有错误。
答案 4 :(得分:0)
你需要实现一些东西
-(IBAction) pickphoto
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[yourView presentModalViewController:picker animated:YES];
picker.videoQuality = UIImagePickerControllerQualityTypeHigh;
picker.allowsEditing = NO;
[picker release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage : (UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
myImage.image=image;
}