Установить UIImageView в качестве пользовательского UIImagePickerController Вид камеры
Я пытаюсь наложить пользовательский вид поверх UIImagePickerController. Представление появляется, когда тип источника - "камера". Тем не менее, UIImageView черный / пустой - поэтому проблема заключается в отображении живого контента в конкретный UIView.
Я посмотрел на этот пост, но я следовал учебнику Apple и использовал presentViewController
в то время как суть ссылочного сообщения SO заключалась в том, что пользователь добавил UIImagePickerController в качестве подпредставления.
Это мой класс:
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
Main view controller for the application.
*/
#import "SnapItViewController.h"
#import "FirstViewController.h"
#import "CollectionListViewController.h"
#import <sys/utsname.h>
#define kWeakDarkenedBackgroundAlpha 0.47
#define kStrongDarkenedBackgroundAlpha 0.65
@interface SnapItViewController ()
@property (nonatomic) IBOutlet UIView *overlayView;
@end
@implementation SnapItViewController
@synthesize fetchedResultsController;
@synthesize currentLocation;
//@synthesize delegate;
@synthesize overlayView;
#pragma mark View lifecycle
- (void)awakeFromNib
{
// Listen to takePicture notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TakePicture) name:@"takePicture" object:nil];
[super awakeFromNib];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
}
- (void)viewWillAppear:(BOOL)animated {
// AVCapturEP
[super viewWillAppear:animated];
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
ipc.showsCameraControls = NO;
/*Load the overlay view from the OverlayView nib file. Self is the File's Owner for the nib file, so the overlayView outlet is set to the main view in the nib. Pass that view to the image picker controller to use as its overlay view, and set self's reference to the view to nil.*/
[[NSBundle mainBundle] loadNibNamed:@"SnapItCameraView" owner:self options:nil];
self.overlayView.frame = ipc.cameraOverlayView.frame;
ipc.cameraOverlayView = self.overlayView;
self.overlayView = nil;
[self presentViewController:ipc animated:YES completion:NULL];
}else{
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//launch image picker view controller
[self presentViewController:ipc animated:YES completion:nil];
}
self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - ImagePickerController Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"didFinishPickingMedia :)");
UIImage* theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// NSData* imageData = UIImageJPEGRepresentation(theImage, 1);
if( picker.sourceType == UIImagePickerControllerSourceTypeCamera )
{
UIImageWriteToSavedPhotosAlbum(theImage, nil, nil, nil);
}
int height = -1;
if([[NSUserDefaults standardUserDefaults] integerForKey:@"reduce_image"] == 0){
height = 640;
} else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"reduce_image"] == 1) {
height = 1024;
} else {
height = 1600;
}
UIImage* resizedImageForUpload = [UtilityFunctions scaleAndRotateImage:theImage maxResolution:height];
NSData* imageDataForUpload = UIImageJPEGRepresentation(resizedImageForUpload, 1); // reduced image! //
NSString *userDataset = [UtilityFunctions retrieveFromUserDefaults:@"dataset"];
[self didPickImage:imageDataForUpload atLocation:currentLocation
userDataset: userDataset];
[picker dismissViewControllerAnimated:YES completion:nil];
[mLocationManager stopUpdatingLocation];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
/*navigate to home tab*/
[self.tabBarController setSelectedIndex:0];
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)pushChildControllerForCollectedLeaf:(CollectedLeaf*)theCollectedLeaf imageToUpload:(NSData*)imageToUpload animated:(BOOL)animated
{
CollectionDetailDataViewController* childController = [[[CollectionDetailDataViewController alloc]initWithNibName:@"CollectionDetailDataViewController" bundle:nil] autorelease];
childController.collectedLeaf = theCollectedLeaf;
//// Pass the image from image picker to Collection Detail View, and it'll handles the upload. ////
//// Set to nil for existing collections. ////
if (imageToUpload)
{
childController.imageToUpload = imageToUpload;
}
childController.hidesBottomBarWhenPushed = YES;
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationController pushViewController:childController animated:animated];
}
- (void)pushChildControllerForCollectedLeaf:(CollectedLeaf*)theCollectedLeaf imageToUpload:(NSData*)imageToUpload
{
[self pushChildControllerForCollectedLeaf:theCollectedLeaf imageToUpload:imageToUpload animated:YES];
}
#pragma mark -
#pragma mark Leaflet Photo Picker Delegate
- (void)didPickImage:(NSData*)imageData atLocation:(CLLocation*)cLocation userDataset:(NSString *)userDataset
{
//// Creates a new Collected Leaf ////
CollectedLeaf* collectedLeaf = (CollectedLeaf*)[NSEntityDescription insertNewObjectForEntityForName:@"CollectedLeaf" inManagedObjectContext:[self.fetchedResultsController managedObjectContext]];
//// Stores the geo-location to mCollectedLeaf ////
if (cLocation)
{
collectedLeaf.latitude = [NSString stringWithFormat:@"%f", cLocation.coordinate.latitude];
collectedLeaf.longitude = [NSString stringWithFormat:@"%f", cLocation.coordinate.longitude];
collectedLeaf.altitude = [NSString stringWithFormat:@"%f", cLocation.altitude];
}
else
{
collectedLeaf.latitude = kGeoLocationNotAvailable;
collectedLeaf.longitude = kGeoLocationNotAvailable;
collectedLeaf.altitude = kGeoLocationNotAvailable;
}
collectedLeaf.collectedDate = [NSDate date];
collectedLeaf.selectedSpecies = kUnknownSpecies;
collectedLeaf.userDataset = userDataset;
[self pushChildControllerForCollectedLeaf:collectedLeaf imageToUpload:imageData animated:YES];
// leafletPhotoPicker = nil;
}
#pragma mark -
#pragma mark Fetched Results Controller Methods
- (NSFetchedResultsController *)fetchedResultsController
{
if (fetchedResultsController != nil)
{
return fetchedResultsController;
}
NSManagedObjectContext* context = [(LeafletAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
// Create and configure a fetch request with the Species entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CollectedLeaf" inManagedObjectContext:context] ;
[fetchRequest setEntity:entity];
// Create the sort descriptors array.
NSSortDescriptor *idDescriptor = [[NSSortDescriptor alloc] initWithKey:@"leafID" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:idDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
fetchedResultsController.delegate = self;
// Memory management.
[fetchRequest release];
[idDescriptor release];
[sortDescriptors release];
return fetchedResultsController;
}
- (IBAction)takePhoto:(id)sender {
[ipc takePicture];
}
- (IBAction)albumView:(id)sender {
UIImagePickerController *imagepickercontrol = [[UIImagePickerController alloc] init];
imagepickercontrol.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate = self;
[self presentViewController:ipc animated:YES completion:nil];
}
- (void)dealloc {
[super dealloc];
}
@end
Как мне скоординировать мой UIImageView с живым представлением камеры UIImagePickerController?