【问题标题】:Camera/Gallery Button in One scene, Image in Another一个场景中的相机/图库按钮,另一个场景中的图像
【发布时间】:2016-05-11 12:56:26
【问题描述】:

我是 Swift 和 iOS 编程的新手,所以请多多包涵。

现在我有 2 个场景,查看控制器和编辑照片。 Image

在视图控制器中,我有 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);
    }

任何帮助将不胜感激。如果我对这一切都错了,请告诉我。谢谢你。

【问题讨论】:

  • 我有 Objective-c 代码,如果你想要我发布答案并且你明白了然后在 swift 中转换代码,它工作正常
  • @lyyappan Ravi 是的,我将不胜感激。谢谢。
  • 我发布了目标 C 代码?

标签: ios xcode swift camera uiimageview


【解决方案1】:

我的代码是 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];
        }

    }

}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-07
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多