gpt4 book ai didi

objective-c - 如何在 View 中保存 UIImage

转载 作者:行者123 更新时间:2023-11-28 22:43:37 24 4
gpt4 key购买 nike

在我的应用程序中,我有两个 View :一个 mapView 和一个详细 View 。 mapView 有注释,注释 View 有正确的标注按钮,当按下标注按钮时,这些按钮会将详细 View 推送到堆栈上。在详细 View 中,用户可以拍摄显示在详细 View Controller 上的 UIImage 上的照片。我想要的是在每个注释的细节 View Controller 上显示不同的图像。我读到我可以使用单例,我也做了一个单例类,但我不知道如何使用它。有人可以帮我吗?这是一些代码:

我的 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

这是详细 View Controller :

#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

最佳答案

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

在这个类中只需添加一个 UIImage 属性并为每个注释分配其自定义图像。

然后在呈现详细 View 时,您只需传递注释并访问您定义的图像属性。

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

@property(nonatomic, strong) MapPoint * annotation;

在你展示你的PinViewController之前,给它分配相应的注解,做一些类似的事情

detailedController.annotation = theAnnotation;

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

self.annotation.image = theImage;

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

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

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

关于objective-c - 如何在 View 中保存 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13813410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com