gpt4 book ai didi

ios - 如何从照片库中选择图片并在应用程序中显示系统相机界面?

转载 作者:可可西里 更新时间:2023-11-01 03:37:59 25 4
gpt4 key购买 nike

如何让用户从 Apple 照片库中选择照片?我们如何显示系统相机 UI 以允许用户拍照?

最佳答案

编辑:2016 年 3 月 15 日 - 这是我之前回答的快速版本,如果您正在寻找 objective-c 版本,您可以在下面找到它。

-- swift 银行 --

首先要符合UIImagePickerControllerDelegate协议(protocol)和UINavigationControllerDelegate协议(protocol)

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate

启动图像选择器

func actionLaunchCamera()
{
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
{
let imagePicker:UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
imagePicker.allowsEditing = true

self.presentViewController(imagePicker, animated: true, completion: nil)
}
else
{
let alert:UIAlertController = UIAlertController(title: "Camera Unavailable", message: "Unable to find a camera on this device", preferredStyle: UIAlertControllerStyle.Alert)
self.presentViewController(alert, animated: true, completion: nil)
}
}

实现 UIImagePickerDelegate 协议(protocol)的委托(delegate)方法

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
// create a filepath with the current date/time as the image name
let savePath:String = self.documentsPath()! + "/" + self.presentDateTimeString() + ".png"

// try to get our edited image if there is one, as well as the original image
let editedImg:UIImage? = info[UIImagePickerControllerEditedImage] as? UIImage
let originalImg:UIImage? = info[UIImagePickerControllerOriginalImage] as? UIImage

// create our image data with the edited img if we have one, else use the original image
let imgData:NSData = editedImg == nil ? UIImagePNGRepresentation(editedImg!)! : UIImagePNGRepresentation(originalImg!)!

// write the image data to file
imgData.writeToFile(savePath, atomically: true)

// dismiss the picker
self.dismissViewControllerAnimated(true, completion: nil)
}

func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
// picker cancelled, dismiss picker view controller
self.dismissViewControllerAnimated(true, completion: nil)
}


// added these methods simply for convenience/completeness
func documentsPath() ->String?
{
// fetch our paths
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

if paths.count > 0
{
// return our docs directory path if we have one
let docsDir = paths[0]
return docsDir
}
return nil
}

func presentDateTimeString() ->String
{
// setup date formatter
let dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"

// get current date
let now:NSDate = NSDate()

// generate date string from now
let theDateTime = dateFormatter.stringFromDate(now)
return theDateTime

}

-- objective-c --

编辑:已更新以在尝试启动相机之前检查相机是否可用。还添加了显示如何将 png 照片保存到应用程序沙箱内的文档文件夹的代码。

试一试(假设使用 ARC)。

在 .h 文件中符合委托(delegate)协议(protocol):

@interface MyViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate>

在 .m 文件中启动图像选择器(相机):

-(void)actionLaunchAppCamera
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = YES;

[self presentModalViewController:imagePicker animated:YES];
}else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Camera Unavailable"
message:@"Unable to find a camera on your device."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
alert = nil;
}
}

然后执行委托(delegate)协议(protocol)来处理用户取消事件或保存/编辑/等等照片。

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//This creates a filepath with the current date/time as the name to save the image
NSString *presentTimeStamp = [Utilities getPresentDateTime];
NSString *fileSavePath = [Utilities documentsPath:presentTimeStamp];
fileSavePath = [fileSavePath stringByAppendingString:@".png"];

//This checks to see if the image was edited, if it was it saves the edited version as a .png
if ([info objectForKey:UIImagePickerControllerEditedImage]) {
//save the edited image
NSData *imgPngData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);
[imgPngData writeToFile:fileSavePath atomically:YES];


}else{
//save the original image
NSData *imgPngData = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage]);
[imgPngData writeToFile:fileSavePath atomically:YES];

}

[self dismissModalViewControllerAnimated:YES];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}

还在编辑中添加:以下是引用 Utilities 类的方法,用于获取文档路径和当前日期/时间

+(NSString *)documentsPath:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:fileName];
}


+(NSString *)getPresentDateTime{

NSDateFormatter *dateTimeFormat = [[NSDateFormatter alloc] init];
[dateTimeFormat setDateFormat:@"dd-MM-yyyy HH:mm:ss"];

NSDate *now = [[NSDate alloc] init];

NSString *theDateTime = [dateTimeFormat stringFromDate:now];

dateTimeFormat = nil;
now = nil;

return theDateTime;
}

关于ios - 如何从照片库中选择图片并在应用程序中显示系统相机界面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4812945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com