如何使用 objective c 显示多张导入的照片
How to display multiple imported photos with objective c
我正在尝试制作一个具有类似于 VSCO 应用程序导入页面的应用程序。但我不知道如何在用户从图库中选择多张照片后在视图中显示所有图片,最多应为 10 张照片。
为了获取图像,我使用了这段代码,它适用于一张图片选择:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];
// transfer to the main screen
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PreviewVC *prevVC = (PreviewVC *)[storyboard instantiateViewControllerWithIdentifier:@"PreviewVC"];
passedImage = image;
prevVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:prevVC animated:true completion:nil];
});
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];
// Go to the Main Screen
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PreviewVC *prevVC = (PreviewVC *)[storyboard instantiateViewControllerWithIdentifier:@"PreviewVC"];
// Passing Image to Preview VC
passedImage = image;
prevVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:prevVC animated:true completion:nil];
});
}
在您可以编辑之前使用预览视图显示导入的照片后:
@implementation PreviewVC
// Hide the Status Bar
- (BOOL)prefersStatusBarHidden {
return true;
}
-(UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
UIGraphicsBeginImageContextWithOptions(newSize, true, image.scale);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
如果想要在 PreviewVC
中构建多个图像,则让它启动选取器并将图像数组保存为 属性。该图像数组又成为预览 vc 中 table 视图的数据源。大致轮廓...
// in PreviewVC.m
// in the private interface
@property (nonatomic, weak) IBOutlet UITableView *tableView; // attach this in IB, don't forget to set datasource to this vc
@property (nonatomic, strong) NSArray *images;
- (void)userDidPressPickImageButton:(id)sender {
// launch image picker vc from here
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];
image = [self imageWithImage:image scaledToSize:someSize]; // move this method here, too
// here's the key
[self.images addObject:image];
[self.tableView reloadData];
}
让您的 table 视图数据源回答 numberOfRows 的 self.images.count
,当然,cellForRow 必须使用 self.images[indexPath.row]
配置单元格
我正在尝试制作一个具有类似于 VSCO 应用程序导入页面的应用程序。但我不知道如何在用户从图库中选择多张照片后在视图中显示所有图片,最多应为 10 张照片。
为了获取图像,我使用了这段代码,它适用于一张图片选择:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];
// transfer to the main screen
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PreviewVC *prevVC = (PreviewVC *)[storyboard instantiateViewControllerWithIdentifier:@"PreviewVC"];
passedImage = image;
prevVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:prevVC animated:true completion:nil];
});
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];
// Go to the Main Screen
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PreviewVC *prevVC = (PreviewVC *)[storyboard instantiateViewControllerWithIdentifier:@"PreviewVC"];
// Passing Image to Preview VC
passedImage = image;
prevVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:prevVC animated:true completion:nil];
});
}
在您可以编辑之前使用预览视图显示导入的照片后:
@implementation PreviewVC
// Hide the Status Bar
- (BOOL)prefersStatusBarHidden {
return true;
}
-(UIImage *)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a bitmap context.
UIGraphicsBeginImageContextWithOptions(newSize, true, image.scale);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
如果想要在 PreviewVC
中构建多个图像,则让它启动选取器并将图像数组保存为 属性。该图像数组又成为预览 vc 中 table 视图的数据源。大致轮廓...
// in PreviewVC.m
// in the private interface
@property (nonatomic, weak) IBOutlet UITableView *tableView; // attach this in IB, don't forget to set datasource to this vc
@property (nonatomic, strong) NSArray *images;
- (void)userDidPressPickImageButton:(id)sender {
// launch image picker vc from here
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:false completion:nil];
image = [self imageWithImage:image scaledToSize:someSize]; // move this method here, too
// here's the key
[self.images addObject:image];
[self.tableView reloadData];
}
让您的 table 视图数据源回答 numberOfRows 的 self.images.count
,当然,cellForRow 必须使用 self.images[indexPath.row]