简体   繁体   English

相机/图库按钮在一个场景中,图像在另一个场景中

[英]Camera/Gallery Button in One scene, Image in Another

I'm new to Swift and iOS programming so please bear with me. 我是Swift和iOS编程的新手,所以请耐心等待。

Right now I have 2 scenes, View Controller and Edit Photo. 现在我有2个场景,View Controller和Edit Photo。 Image 图片

In View Controller I have 2 buttons, one to take a photo using the iPhone's camera and another to choose a photo from the gallery. 在View Controller中,我有2个按钮,一个用于使用iPhone的相机拍摄照片,另一个用于从图库中选择照片。 The code works where if I press either button it functions properly. 代码适用于如果我按任一按钮它正常运行。 However, my problem is where the image is sent after it has been taken/selected. 但是,我的问题是在拍摄/选择图像后发送图像。

What I want it to do is after the user takes the photo or selects it from the gallery, I want the image to have it's own view so that it can be edited afterwards. 我想要它做的是在用户拍摄照片或从图库中选择它之后,我希望图像具有自己的视图,以便之后可以对其进行编辑。 I'm creating a meme app, so I want the image to have the screen to itself. 我正在创建一个meme应用程序,所以我希望图像能够拥有自己的屏幕。

This is what I can't get working. 这是我无法工作的。 I can put a UIImageView on the same view as the buttons and it works that way, but I can't make the image go onto its own view. 我可以将UIImageView放在与按钮相同的视图上,它可以按照这种方式工作,但我无法使图像进入自己的视图。

View Controller Code 查看控制器代码

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)
        }
    }
}

The code below is what I want in the new view. 下面的代码是我在新视图中想要的代码。 I tried creating an outlet from the ImageView to the EditPhoto.swift file, but the scenes don't connect afterwards. 我尝试从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);
    }

Any help would be appreciated. 任何帮助,将不胜感激。 And if I'm going about this all wrong, please let me know. 如果我发现这一切都错了,请告诉我。 Thank you. 谢谢。

My code is Objective C, i post its, you will get the idea and its working for me, camera image and gallery image, 我的代码是Objective C,我发布了它,你会得到这个想法,它为我工作,相机图像和图库图像,

imageAddView is UIImageView , imageAddView是UIImageView

my gallery get image method is below, 我的画廊获取图像方法如下,

-(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;
}

My camera capture method is below, 我的相机拍摄方法如下,

-(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];
        }

    }

}

hope its helpful. 希望它有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 image_picker:如果首先访问相机,则取消按钮在图库中不可见 - image_picker: Cancel button not visible in gallery, if camera was accessed first iOS如何通过编程方式从iPhone相机场景中获取图像而无需按下拍摄按钮? - IOS How to get image from IPhone camera scene by programmatically without press shoot button? 我需要将一个游戏场景中的图像从Swift中的另一个游戏场景中切换 - I need to switch an image that is on one game scene from another game scene in Swift 裁剪并调整从图库或照相机中选择的图像的大小 - crop and resize image that is selected from gallery or camera Alamofire在Swift 3中上传的相机和图库图片 - Camera and gallery image upload by alamofire in swift 3 将按钮或图像从一个点旋转到另一个 - Rotate Button Or Image From One Point To another 在Xcode的另一个场景中使用按钮暂停音乐 - Pause music with a button in another scene in Xcode 如何在iPhone中将图像从一个按钮拖到另一个按钮? - How to drag a image from one button to another button in iPhone? 将INT值从一个场景传递到另一个场景 - Pass INT value from one Scene to another 如何通过单击相机覆盖上的按钮打开照片库? - How to open photo gallery by clicking a button from a camera overlay?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM