我是Swift和iOS编程的新手,所以请耐心等待。
现在我有2个场景,View Controller和Edit Photo。 Image
在View Controller中,我有2个按钮,一个用于使用iPhone相机拍摄照片,另一个用于从图库中选择照片。代码适用于如果我按任一按钮它正常运行。但是,我的问题是在拍摄/选择图像后发送图像。
我想要它做的是在用户拍摄照片或从图库中选择照片后,我希望图像具有自己的视图,以便之后可以对其进行编辑。我正在创建一个meme应用程序,所以我希望图像能够拥有自己的屏幕。
这是我无法工作的。我可以将UIImageView放在与按钮相同的视图上,它可以按照这种方式工作,但我无法使图像进入自己的视图。
查看控制器代码
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate,
UINavigationBarDelegate, UINavigationControllerDelegate
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Opens the camera
@IBAction func openCameraButton(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
//Opens the photo gallery
@IBAction func openGalleryButton(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
imagePicker.allowsEditing = true
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
}
以下代码是我在新视图中想要的代码。我尝试从ImageView创建一个插件到EditPhoto.swift文件,但之后场景不会连接。
//Saves the image
@IBOutlet weak var imagePicked: UIImageView!
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
imagePicked.image = image
self.dismissViewControllerAnimated(true, completion: nil);
}
任何帮助将不胜感激。如果我发现这一切都错了,请告诉我。谢谢。
答案 0 :(得分:0)
我的代码是Objective C,我发布了它,你会得到这个想法及其为我工作,相机图像和图库图像,
imageAddView是UIImageView
,
我的图库获取图片方法如下,
-(void)addphotos
{
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
photoPicker.allowsEditing= YES;
[self presentViewController:photoPicker animated:YES completion:NULL];
isCaptureImage = NO;
}
我的相机拍摄方法如下,
-(void) TakeNewphoto
{
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Device has no camera"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[myAlertView show];
}
else
{
UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init];
photoPicker.delegate = self;
photoPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
photoPicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
photoPicker.allowsEditing = YES;
[self presentViewController:photoPicker animated:YES completion:NULL];
isCaptureImage = YES;
}
}
- (void)imagePickerController:(UIImagePickerController *)photoPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType;
mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage ])
{
if (isCaptureImage == NO)
{
ALAssetsLibrary *library = [ALAssetsLibrary new];
[library assetForURL:[info valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *original = [asset defaultRepresentation];
UIImage *myimag = [UIImage imageWithCGImage:[original fullScreenImage] scale:[original scale] orientation:0];
imageAddView.image = [UIImage squareImageWithImage:myimag scaledToSize:CGSizeMake(640, 640)];
} failureBlock:^(NSError *error) {
NSLog(@"Failed to load captured image.");
}];
[photoPicker dismissViewControllerAnimated:YES completion:Nil];
}
else
{
UIImage * image = (UIImage*) [info objectForKey:UIImagePickerControllerEditedImage];
imageAddView.image = [UIImage squareImageWithImage:image scaledToSize:CGSizeMake(640, 640)];
[photoPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
}
希望它有所帮助。