我目前正在为 iOS 构建一个应用程序。我正在尝试保存用户当前正在查看的内容,以便我可以为下一个 View 加载它。我知道如何通过
保存图像 CGSize size = [self.tableView bounds].size;
UIGraphicsBeginImageContext(size);
[[self.tableView layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
但是我不知道如何将它们移动到下一个 View Controller 。出于某种原因,上面拒绝将其加载到下一个 View Controller 中的 ImageView 中。
已解决
_incommingImage 是 ImageView 。
我是这样保存的
NSData *imageData = UIImageJPEGRepresentation(newImage, 1.0);
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [[pathArr objectAtIndex:0]
stringByAppendingPathComponent:@"img.data" ];
[imageData writeToFile:path atomically:YES];
然后我用这个加载它。
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [[pathArr objectAtIndex:0]
stringByAppendingPathComponent:@"img.data" ];
NSData *retrievedData = [NSData dataWithContentsOfFile:path];
_incommingImage.image = [UIImage imageWithData:retrievedData];
[_incommingImage release];
最佳答案
我同意罗伯特的观点。
FirstViewController.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"Your_Identifier"]) {
SecondViewController *secondVC = segue.destinationViewController;
secondVC.incommingImage = myImage;
}
}
SecondViewController.h:
@property (strong, nonatomic) UIImage *incommingImage;
一旦您的 SecondViewController 出现,图像将自动设置为 incommingImage。请务必在 Storyboard 上设置 Segue 的标识符,使其与prepareForSegue 下的标识符相匹配。
关于ios - 在 segue 上移动图像以加载到另一个 UIImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24661381/