我已经在我的ViewDidLoad中尝试过:
NSString *myGrabbedImage = @"myGrabbedImage.png";
NSString *myGrabbedImage2 = @"myGrabbedImage2.png";
NSString *myGrabbedImage3 = @"myGrabbedImage3.png";
NSString *myGrabbedImage4 = @"myGrabbedImage4.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *path2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *path3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *path4 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *documentDirectory2 = [path2 objectAtIndex:0];
NSString *documentDirectory3 = [path3 objectAtIndex:0];
NSString *documentDirectory4 = [path4 objectAtIndex:0];
NSString *fullPath = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
NSString *fullPath2 = [documentDirectory2 stringByAppendingPathComponent:myGrabbedImage2];
NSString *fullPath3 = [documentDirectory3 stringByAppendingPathComponent:myGrabbedImage3];
NSString *fullPath4 = [documentDirectory4 stringByAppendingPathComponent:myGrabbedImage3];
NSData *data = [NSData dataWithContentsOfFile:fullPath];
NSData *data2 = [NSData dataWithContentsOfFile:fullPath2];
NSData *data3 = [NSData dataWithContentsOfFile:fullPath3];
NSData *data4 = [NSData dataWithContentsOfFile:fullPath4];
if ([myGrabbedImage isEqualToString:@"myGrabbedImage.png"])
{
[[self firstImageView]setImage:[UIImage imageWithData:data]];
}
else if ([myGrabbedImage2 isEqualToString:@"myGrabbedImage2.png"])
{
[[self secondImageView]setImage:[UIImage imageWithData:data2]];
}
else if ([myGrabbedImage3 isEqualToString:@"myGrabbedImage3.png"])
{
[[self thirdImageView]setImage:[UIImage imageWithData:data3]];
}
else
{
[[self fourthImageView]setImage:[UIImage imageWithData:data4]];
}
然后我的UIImagePickerController方法中有这个:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image1 = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data = UIImagePNGRepresentation(image1);
NSString *myGrabbedImage = @"myGrabbedImage.png";
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [path objectAtIndex:0];
NSString *fullPathToFile = [documentDirectory stringByAppendingPathComponent:myGrabbedImage];
[data writeToFile:fullPathToFile atomically:YES];
if (picker == imagePickerController)
{
[[self firstImageView]setImage:image1];
}
else if (picker == imagePickerController2)
{
[[self secondImageView]setImage:image1];
}
else if (picker == imagePickerController3)
{
[[self thirdImageView]setImage:image1];
}
else if (picker == imagePickerController4)
{
[[self fourthImageView]setImage:image1];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
粗略而言,ViewDidLoad仅显示一张图像。但是,如何使它加载所有4个UIImageView。
提前致谢
最佳答案
#import <UIKit/UIKit.h>
@interface PickerController: UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
NSArray *titleStrings;
UIPickerView *picker;
UIImage *imageView;
}
@end
@implementation PickerController
- (id) init {
if (self = [super init]) {
titleStrings = [[NSArray alloc] initWithObjects:@"A", @"B", NULL];
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 100)];
picker.showsSelectionIndicator = YES;
picker.delegate = self;
picker.dataSource = self;
[self.view addSubview:picker];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty.png"]];
imageView.frame = CGRectMake(0, 200, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
[imageView release];
[picker release];
}
return self;
}
- (void) dealloc {
[titleStrings release];
[super dealloc];
}
/* UIPickerViewDataSource */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
return 2;
}
/* UIPickerViewDelegate */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [titleStrings objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:( NSInteger)component {
NSString *first = [titleStrings objectAtIndex:[pickerView selectedRowInComponent:0]];
NSString *second = [titleStrings objectAtIndex:[pickerView selectedRowInComponent:1]];
NSString *imageName = [[NSString alloc] initWithFormat:@"%@%@", first, second];
imageView.image = [UIImage imageNamed:imageName];
[imageName release];
[imageView sizeToFit];
}
@end
关于ios - ViewDidLoad加载4张图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13729893/