图像不保留在secondViewController上

时间:2014-05-23 10:29:59

标签: ios objective-c uiimageview uiimage

我有3个ViewControllers,第一个加载UIImagePickerController第二个显示从ViewControllers中的UIImagePickerController UIImageView中选取的图像,并且有一个加载按钮thirdViewController,此ViewController的图片显示在UIImageView,我想加载secondViewController UIImageViewMFMailComposeViewController,但我想要的图片能够加载button移动和拉伸,然后保存到位图并使用thirdViewController传递给电子邮件,我已经为其写了一个模板(电子邮件工作)

我遇到的问题是 -

  • 当我按secondViewController加载firstViewController's并返回UIImagePickerController时,thirdViewController secondViewController中所选的图片已消失(未被保留)(已解决)

  • 在使用UIImageView secondViewController中的图片方式时,我很失落,因为我不知道如何将它们变成加载按钮的按钮将图片放入#import <UIKit/UIKit.h> #import <MobileCoreServices/MobileCoreServices.h> #import "secondViewController.h" @interface spViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> @property BOOL newMedia; @property (strong, nonatomic) IBOutlet UIImageView *imageView; @property (strong, nonatomic) spViewController *secondViewController; @property (strong, nonatomic) UIImageView *image; @property (strong, nonatomic) UIImage *myImage; - (IBAction)useCamera:(id)sender; - (IBAction)useCameraRoll:(id)sender; @end

  • 中的#import "spViewController.h" #import "secondViewController.h" @interface spViewController () @end @implementation spViewController @synthesize myImage; - (IBAction)backtohome:(UIStoryboardSegue *)unwindSegue { } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)useCamera:(id)sender { if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.mediaTypes = @[(NSString *) kUTTypeImage]; imagePicker.allowsEditing = YES; [self presentViewController:imagePicker animated:YES completion:nil]; _newMedia = NO; } } - (IBAction)useCameraRoll:(id)sender { if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate = self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.mediaTypes = @[(NSString *) kUTTypeImage]; imagePicker.allowsEditing = YES; //change NO to YES [self presentViewController:imagePicker animated:YES completion:nil]; _newMedia = NO; } } //image picker delegate -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *chosenImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage]; //it returns the edited image NSString *mediaType = info[UIImagePickerControllerMediaType]; self.myImage = chosenImage; if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { UIImage *image = info[UIImagePickerControllerOriginalImage]; _imageView.image = image; if (_newMedia) { } else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) { } } [self performSelector:@selector(myMethod:) withObject:picker afterDelay:0.5]; } -(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]; } } //cancel delegate -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:nil]; } -(void)myMethod:(UIImagePickerController *)picker { [picker dismissViewControllerAnimated:YES completion:^{ NSLog(@"Perform segue"); [self performSegueWithIdentifier:@"Picture Unwind Segue" sender:self]; }]; } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier]isEqualToString:@"Picture Unwind Segue"]) { secondViewController *destinationViewController = [segue destinationViewController]; destinationViewController.myImage = self.myImage; //just set the image of secondViewController } } @end
  • 一旦图像全部合并到所述位图中,我不知道用于在图像中发布保存的位图的代码

提前感谢您的答案,这里有一些代码:

firstViewController.h

#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "spViewController.h"
#import <MessageUI/MessageUI.h>
#import "selectselfViewController.h"

@interface secondViewController : UIViewController

<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>


@property (strong, nonatomic) UIImage *myImage;
@property (strong) UIImageView *Viewofimage;
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

- (IBAction)openShelfPage:(id)sender;
- (IBAction)emailSupport:(id)sender;


@end

firstViewController.m

#import "secondViewController.h"
#import "spViewController.h"

@interface secondViewController ()

@end

@implementation secondViewController
@synthesize myImage;



- (IBAction)emailSupport:(id)sender
{
    NSLog(@"sender %@",[sender description]);


    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    mailComposer.mailComposeDelegate = self;
    [mailComposer setToRecipients:[NSArray arrayWithObjects: @"info@bespokenature.co.uk",nil]];
    [mailComposer setSubject:@"Request for quote"];
    NSString *supportText = [NSString stringWithFormat:@"Please state the dimentions you would like, the number of shelfs and the colour"];
    supportText = [supportText stringByAppendingString: @"add any further details here"];
    [mailComposer setMessageBody:supportText isHTML:NO];
    [self presentViewController:mailComposer animated:YES completion:nil];


}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissViewControllerAnimated:YES completion:nil];
}



- (IBAction)openShelfPage:(id)sender
{}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _imageView.image = self.myImage;
    _imageView.contentMode = UIViewContentModeScaleAspectFit;



}

    // Do any additional setup after loading the view.


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

}


@end

secondViewController.h

#import <UIKit/UIKit.h>

@interface selectselfViewController : UIViewController

- (IBAction)backtoSVC:(id)sender;

@end

secondViewController.m

#import "selectselfViewController.h"

@interface selectselfViewController ()

@end

@implementation selectselfViewController

- (IBAction)backtoSVC:(id)sender{

    [self dismissViewControllerAnimated:YES completion:nil];}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

thirdViewController.h

UIImagePickerController

thirdViewController.m

UIImagePickerController

再次谢谢

编辑:我添加了我的应用程序中的所有代码,以便了解更多信息。

编辑2:我在下面添加了一些信息来解释应用程序和问题好一点

应用的功能

我被要求为一家木工公司制作这个应用程序,专门用于“浮动货架”,他们想要一个用户可以拍摄墙壁照片,然后从货架目录中挑选,将货架放在墙上(数量众多)货架伸展并适合尺寸然后按一个按钮并保存整个图像(包括货架)并通过电子邮件发送给公司所有者,以便他们可以发回报价。

远远

  • 我已设置按钮以打开secondViewController
  • 我已经制作了以便应用显示我thirdViewController
  • thirdViewController拍摄的照片
  • 我设置了一个按钮,可以转到UIImageViews
  • 我已将用户选中的货架放在thirdViewController UIImageView

我没有做过的事情

  • MFMailComposeViewController可选择的货架图像放置在显示墙壁图片的{{1}}上

  • 保存图片以备导入电子邮件({{1}})

1 个答案:

答案 0 :(得分:0)

关注问题#2 我们可能需要关于问题#3的更多信息,我不清楚你的意思是什么'post',也许它应该是它自己的问题。

问题#2

我假设你正在使用UINavigationController。您必须了解导航控制器具有一堆视图控制器。当您第一次启动应用程序并进入FirstViewController时,堆栈如下所示:
| --- FirstViewController --- |

当您转到第二个视图控制器时,第一个视图控制器仍在内存中,但不在屏幕上。堆栈看起来像这样:
| --- FirstViewController --- | --- SecondViewController --- |

再次转到第三个视图控制器时,第一个和第二个视图控制器仍在内存中,但不在屏幕上。堆栈看起来像这样:
| --- FirstViewController --- | --- SecondViewController --- | --- ThirdViewController --- |

你通过控制从第三个视图控制器拖动到第二个视图控制器实际上做了什么(可以这样做但是你的问题暗示我不知道Unwind Segues,所以我假设你只是使用正常的segue:) | --- FirstViewController --- | --- SecondViewController --- | --- ThirdViewController --- | ---的 SecondViewController --- |

实际上,你要做的是从内存中“删除”第三个视图控制器,但告诉你的第二个视图控制器在第三个视图控制器中发生了什么。您可以通过多种方式实现这一目标......我个人的偏好是使用块。如果你愿意,你可以使用ThirdViewController委托,但在我看来这是太多的开销。您可以在第三个视图控制器上添加块作为属性,并在第二个视图控制器的prepareForSegue:sender:方法中实现该块。在第三个视图控制器中有类似的东西...

@property (nonatomic, copy) void(^onImageSelected)(UIImage *image);

...以及第二个视图控制器中的相应代码:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    ThirdViewController *thirdViewController = (ThirdViewController *)segue.destinationViewController;
    [thirdViewController setOnImageSelected:^(UIImage *image) {
        // Do something here
        // This is the call back
    }];
}

确保在第三个ViewController中选择图像或用户按下时调用块(并在调用之前检查它是否为nil)。然后你将调用放在dealloc方法中:

// Image was selected
if (self.onImageSelected)
    self.onImageSelected(selectedImage);