我在下面发布的代码有两个功能;第一个是拍照(工作正常),第二个是从库中挑选图像(不起作用)。这些函数没有被正确调用。请检查我的代码并让我知道出了什么问题。
提前致谢。
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
static NSDateFormatter *dateFormatter = nil;
NSString *stringDate = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EE, d LLLL yyyy"];
stringDate = [dateFormatter stringFromDate:[NSDate date]];
}
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// storing to camera roll
UIImageWriteToSavedPhotosAlbum(image,self,@selector(image:finishedSavingWithError:contextInfo:),nil);
CGSize newSize=CGSizeMake(320, 436);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
xapp.profileImage=newImage;
[[NSNotificationCenter defaultCenter]postNotificationName:@"addImage" object:nil];
}
// Commit the change.
[self dismissModalViewControllerAnimated:YES];
}
-(void)image:(UIImage *)image finishedSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Save failed"
message: @"Failed to save image"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}
最佳答案
嗨,我刚刚放置了从照片库的相机完全捕获图像的代码,只需检查一下并尝试在您的项目中实现:-
-(IBAction)actionImage:(id)sender
{
UIActionSheet *option =[[UIActionSheet alloc]initWithTitle:@"Select" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select Photo",@"Take Photo",nil];
option.actionSheetStyle =UIActionSheetStyleDefault;
[option showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag==2)
{
[self.navigationController popViewControllerAnimated:YES];
}
else
{
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if(buttonIndex ==0)
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
}
}
else if(buttonIndex ==1)
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
}
}
}
#pragma mark - imagePickerController Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
imgUserImage.image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
return;
}
注意:- 不要忘记包含相关的框架或委托(delegate)。 希望它能帮助您不需要额外的编码,您只需使用上述方法即可完成:)
关于iphone - 从我的图库添加照片时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476612/