如何在视图中保存图像

时间:2012-12-11 03:59:21

标签: objective-c ios uiimage mapkit

在我的应用程序中,我有两个视图:mapView和详细视图。 mapView具有注释,注释视图具有正确的标注按钮,可在按下标注按钮时将详细视图推送到堆栈。在详细视图中,用户可以拍摄出现在详细视图控制器上的UIImage上的图片。我想要的是每个注释的细节视图控制器上的不同图像。我已经读过我可以使用单例,我已经创建了一个单例类,但我不知道如何使用它。有人可以帮我吗? 这是一些代码:

我的MKAnnotation课程:

#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface MapPoint : NSObject <MKAnnotation>
{
    NSString *_address;
    CLLocationCoordinate2D _coordinate;
    NSNumber *_identifier;
    UIImage *_image;
}

- (id)initWithAddress:(NSString*)address
    coordinate:(CLLocationCoordinate2D)coordinate
         title:(NSString *)t
    identifier:(NSNumber *)ident;


//This is a required property from MKAnnotation
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

//This is an optional property from MKAnnotataion
@property (nonatomic, copy) NSString *title;
@property (nonatomic, readonly, copy) NSString *subtitle;
@property (nonatomic) BOOL animatesDrop;
@property (nonatomic) BOOL canShowCallout;

@property (copy) NSString *address;
@property (copy) NSNumber *identifier;
@property (copy,nonatomic) UIImage *image;

@end

@implementation MapPoint

@synthesize title, subtitle, animatesDrop, canShowCallout;
@synthesize address = _address, coordinate = _coordinate, identifier = _identifier, image = _image;

-(id)initWithAddress:(NSString *)address
   coordinate:(CLLocationCoordinate2D)coordinate
        title:(NSString *)t
   identifier:(NSNumber *)ident
{
    self = [super init];

    if (self) {
        _address = [address copy];
        _coordinate = coordinate;
        _identifier = ident;

        [self setTitle:t];

        NSDate *theDate = [NSDate date];

        subtitle = [NSDateFormatter localizedStringFromDate:theDate
                                              dateStyle:NSDateFormatterMediumStyle
                                              timeStyle:NSDateFormatterMediumStyle];
    }
    return self;
}

@end

这是详细视图控制器:

#import <UIKit/UIKit.h>
@class P2OViewController;

@interface PinViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate,UITextFieldDelegate>

{
    __weak IBOutlet UILabel *label;
    __weak IBOutlet UIButton *removeButton;
    NSString *addressForLabel;
    __weak P2OViewController *_vc;
    __weak NSNumber *_identifier;
}

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *removeButton;
@property (weak, nonatomic) P2OViewController *vc;
@property (weak, nonatomic) NSNumber *identifier;

-(void)takePicture:(id)sender;

-(void)buttonPressed:(id)sender;

@end

#import "PinViewController.h"
#import "MapPoint.h"
#import "P2OViewController.h"

@implementation PinViewController

@synthesize imageView, label, removeButton;
@synthesize vc = _vc, identifier = _identifier;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        UIBarButtonItem *pic = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
                                                                        target:self
                                                                        action:@selector(takePicture:)];
    [[self navigationItem]setRightBarButtonItem:pic];

}
return self;
}

-(void)takePicture:(id)sender
    {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];

    //If our device has a camera, we ant to take a picture, otherwise, we just pick from photo library
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    } else {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    [imagePicker setDelegate:self];

    //Place the image picker on the screen
    [self presentViewController:imagePicker
                   animated:YES
                 completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //Get picked image from info dictionary
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

    //Put the image onto the screen in out image view
    [imageView setImage:image];

    //Take image picker off the screen - you must call this dismiss method
    [self dismissViewControllerAnimated:YES
                         completion:nil];

}

@end

1 个答案:

答案 0 :(得分:2)

您肯定有一个实现MKAnnotation协议的类。

在此课程中,只需添加UIImage属性,并为每个注释指定其自定义图像。

然后在展示详细视图时,您只需传递注释并访问其定义的图像属性。

修改 在您的PinViewController中,只需声明一个像这样的属性

@property(nonatomic, strong) MapPoint * annotation;

在您展示PinViewController之前,为其指定相应的注释,执行类似

的操作
detailedController.annotation = theAnnotation;

做任何你需要做的事情来获取图像,然后以这种方式将它存储在注释中

self.annotation.image = theImage;

现在注释正在存储图像,因此只要您将其保存在内存中,就可以使用它。

下次推送详细视图时,您可以检查该注释是否已有图像

if(nil != self.annotation.image) {
     // do something
} else {
     // do something else
}