How to display multiselected images in iOS - ios

I am making an app for iPhone and want to give users the ability to multiselect images from their photo-library. I already have a working code for user to select four images at a time.
But I can't select 1 or 2 or 3 images at a time. I want to select the 1 or 2 or 3 images at a time.
This is my code. I have struggled for 3 days without finding a solution. Please help me anybody.
Thanks in advance.............
-(void)choosePhotoFromExistingImages
{
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
elcPicker.maximumImagesCount = 4;
elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
elcPicker.imagePickerDelegate = self;
[self presentViewController:elcPicker animated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
}
- (void)displayPickerForGroup:(ALAssetsGroup *)group
{
ELCAssetTablePicker *tablePicker = [[ELCAssetTablePicker alloc] initWithStyle:UITableViewStylePlain];
tablePicker.singleSelection = YES;
tablePicker.immediateReturn = YES;
ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initWithRootViewController:tablePicker];
elcPicker.maximumImagesCount = 0;
elcPicker.imagePickerDelegate = self;
elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
tablePicker.parent = elcPicker;
// Move me
tablePicker.assetGroup = group;
[tablePicker.assetGroup setAssetsFilter:[ALAssetsFilter allAssets]];
[self presentViewController:elcPicker animated:YES completion:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return YES;
}
else
{
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}
}
#pragma mark ELCImagePickerControllerDelegate Methods
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
[self dismissViewControllerAnimated:YES completion:nil];
CGRect workingFrame = _scrollView.frame;
workingFrame.origin.x = 0;
NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
for (NSDictionary *dict in info)
{
UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
[images addObject:image];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[self.view addSubview:imageview ];
self.chosenImages = images;
}
UIImageView *image1=[[UIImageView alloc]initWithFrame:CGRectMake(10, 240, 40, 40)];
image1.image=[images objectAtIndex:0];
[self.view addSubview:image1];
UIImageView *image2=[[UIImageView alloc]initWithFrame:CGRectMake(60, 240, 40, 40)];
image2.image=[images objectAtIndex:1];
[self.view addSubview:image2];
UIImageView *image3=[[UIImageView alloc]initWithFrame:CGRectMake(120, 240, 40, 40)];
image3.image=[images objectAtIndex:2];
[self.view addSubview:image3];
UIImageView *image4=[[UIImageView alloc]initWithFrame:CGRectMake(180, 240, 40, 40)];
image4.image=[images objectAtIndex:3];
[self.view addSubview:image4];
}
- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}

Here is the link for the Tutorial. Its works fine in my project.Try this
Hope its useful for you.

Check your code in - (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
For example, you create array with length of 2, but in code below you try to get image3.image=[images objectAtIndex:2]; and image4.image=[images objectAtIndex:3];. It's not a surprise that your code throws exception.
Good luck!

Related

how to combine overlay and camera image together in iOS?

I want to develop a application in which user can change there hair style. so i add a overlay in camera view by using following code files and try to combine both image.
Here is my ViewController.h file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate>{
UIImagePickerController *imagePicker;
UIPopoverController *popoverController;
NSData *imageData;
}
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#end
and ViewController.m file
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// assign action to button
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 200, 60);
myButton.center = self.view.center;
[myButton addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:#"Image Picker" forState:UIControlStateNormal];
[self.view addSubview:myButton];
}
- (void)buttonPress:(id)sender {
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
// alert the user that the camera can't be accessed
UIAlertView *noCameraAlert = [[UIAlertView alloc] initWithTitle:#"No Camera" message:#"Unable to access the camera!" delegate:nil cancelButtonTitle:#"Dismiss" otherButtonTitles:nil, nil];
[noCameraAlert show];
} else {
// prepare imagePicker view
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
imagePicker.delegate = self;
imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
imagePicker.navigationBarHidden = YES;
imagePicker.toolbarHidden = YES;
imagePicker.extendedLayoutIncludesOpaqueBars = YES;
// create view for overlay
CGRect overlayRect = CGRectMake(0, 0, imagePicker.view.frame.size.width, imagePicker.view.frame.size.height-50);
UIView *overlayView = [[UIView alloc] initWithFrame:overlayRect];
// prepare the image to overlay
UIImageView *overlayImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"wig"]];
overlayImage.center = overlayView.center;
//overlayImage.alpha = 0.5;
[overlayView addSubview:overlayImage];
// add the image as the overlay
[imagePicker setCameraOverlayView:overlayView];
// display imagePicker
[self.navigationController presentViewController:imagePicker animated:YES completion:nil];
}
}
#pragma mark - UIBarButton Selectors
- (void)takePictureButtonPressed:(id)sender {
NSLog(#"takePictureButtonPressed...");
// TODO: take picture!
[self presentViewController:imagePicker animated:YES
completion:^ {
[imagePicker takePicture];
}];
}
- (void)startStopButtonPressed:(id)sender {
NSLog(#"startStopButtonPressed...");
// TODO: make this do something
}
- (void)timedButtonPressed:(id)sender {
NSLog(#"timedButtonPressed...");
// TODO: implement timer before calling takePictureButtonPressed
}
- (void)cancelButtonPressed:(id)sender {
NSLog(#"cancelButtonPressed");
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIImagePickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editingInfo {
[picker dismissViewControllerAnimated:YES completion:NULL];
[popoverController dismissPopoverAnimated: YES];
NSData *image1 = UIImageJPEGRepresentation([editingInfo valueForKey:UIImagePickerControllerOriginalImage],1.0);
UIImage *Imgg = [self addOverlayToBaseImage:[editingInfo valueForKey:UIImagePickerControllerOriginalImage]];
image1 = UIImageJPEGRepresentation(Imgg,1.0);
imageData = image1;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath =[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png",#"cached"]];
NSLog((#"pre writing to file"));
if (![imageData writeToFile:imagePath atomically:NO])
{
NSLog((#"Failed to cache image data to disk"));
}
else
{
[[NSUserDefaults standardUserDefaults] setValue:imagePath forKey:#"imagePath"];
NSLog(#"the cachedImagedPath is %#",imagePath);
}
[self.imageView setImage:Imgg];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIImage*)addOverlayToBaseImage:(UIImage*)baseImage{
UIImage *overlayImage = [UIImage imageNamed:#"wig.png"];
CGPoint topCorner = CGPointMake(0, 0);
CGSize targetSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
CGRect scaledRect = CGRectZero;
CGFloat scaledX = self.view.frame.size.height * baseImage.size.width / baseImage.size.height;
CGFloat offsetX = (scaledX - self.view.frame.size.width) / -2;
scaledRect.origin = CGPointMake(offsetX, 0.0);
scaledRect.size.width = scaledX;
scaledRect.size.height = self.view.frame.size.height;
UIGraphicsBeginImageContext(targetSize);
[baseImage drawInRect:scaledRect];
[overlayImage drawAtPoint:topCorner];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
#end
This code combine both image but i want the exact same image which user can see in camera preview. My problem is when i capture image and see image in UIImageview its totally different from camera preview.
Camera Preview Image
What i get
Help me to get same image like Camera preview.
I was also facing the same issue. I have fixed this issue here you can find my complete git repo
Camera Preview Image
What i get

Custom camera on a UIView, not full screen

I'm doing an application that on a specific part of the application, the user will be able to take a picture from the camera or select from the Album and this image will be shown on the ViewController.
This is kind of easy, but, what I need, that I'm not being able to do is:
-I don't want that the Camera open full screen, I want it to 'open' on the ViewController that the user is, in the exactly same place as where the image will be, inside a circle.
I was able to make the Camera not open full screen, but I'm not being able to put this view inside the circle view and crop the photo according to the circle.
Here is my code, it might work if you create a standard project and I think that will help to understand what's going on:
VARIABLES
#property (nonatomic, strong) UIImagePickerController * picker;
#property (nonatomic, strong) UIImageView * photoView;
#property (nonatomic, strong) UIView * overlayView;
#property (nonatomic, strong) UIButton * takePhotoButton; //- function
#property (nonatomic, strong) UIButton * selectPhotoButton;
#property (nonatomic, strong) UIButton * takePicture; //- action
CODE
#interface CameraViewController ()
{
BOOL isFromAlbum;
}
#end
#implementation CameraViewController
#synthesize photoView;
#synthesize picker;
#synthesize takePicture;
#synthesize selectPhotoButton;
#synthesize takePhotoButton;
#synthesize overlayView;
#synthesize switchCameraButton;
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface
{
[super addBackButton];
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:#"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:#selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
//[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:#"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:#selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
//[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = CGRectMake(0, 0, 320, 320);
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
self.photoView.userInteractionEnabled = YES;
}
#pragma mark -
#pragma mark Button's Actions
- (void)takePhotoFunction
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.view.frame = self.photoView.bounds;
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 70, 70, 70);
[self.takePicture addTarget:self action:#selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:#"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
self.switchCameraButton = [[UIButton alloc] init];
self.switchCameraButton.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.origin.y, 70, 70);
[self.switchCameraButton addTarget:self action:#selector(switchCamera:) forControlEvents:UIControlEventTouchUpInside];
[self.switchCameraButton setBackgroundImage:[UIImage imageNamed:#"reverse-camera-btn"] forState:UIControlStateNormal];
[self.photoView addSubview:self.switchCameraButton];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender
{
[self.picker takePicture];
}
-(IBAction)switchCamera:(id)sender
{
if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
else if (picker.cameraDevice == UIImagePickerControllerCameraDeviceRear)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO)
{
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage)
{
imageToSave = editedImage;
}
else
{
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.switchCameraButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
CGFloat minimumSide = fminf(imageToSave.size.width, imageToSave.size.height);
CGFloat finalSquareSize = 640.;
//create new drawing context for right size
CGRect rect = CGRectMake(0, 0, finalSquareSize, finalSquareSize);
CGFloat scalingRatio = 640.0/minimumSide;
UIGraphicsBeginImageContext(rect.size);
//draw
[imageToSave drawInRect:CGRectMake((minimumSide - originalImage.size.width)*scalingRatio/2., (minimumSide - originalImage.size.height)*scalingRatio/2., originalImage.size.width*scalingRatio, originalImage.size.height*scalingRatio)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//[self.photoView setImage:imageToSave];
[self.photoView setImage:croppedImage];
}
else
{
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker
{
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
With the code below, I get the following results:
A single view with a dark blue background.
2 green circles (the buttons)
Tapping on the left circle puts the camera's "sight" inside of it.
Tapping on the "take" button (at the bottom of the circle on the left) fires off the "takePhotoAction" method.
[Code]
// ViewController.m
#import "ViewController.h"
#interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
BOOL isFromAlbum;
}
#property (nonatomic, strong) UIImagePickerController * picker;
#property (nonatomic, strong) UIImageView * photoView;
#property (nonatomic, strong) UIView * overlayView;
#property (nonatomic, strong) UIButton * takePhotoButton; //- function
#property (nonatomic, strong) UIButton * selectPhotoButton;
#property (nonatomic, strong) UIButton * takePicture; //- action
#end
#implementation ViewController
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad {
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface {
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:#"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:#selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:#"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:#selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = self.takePhotoButton.frame;
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
[self.photoView setUserInteractionEnabled:YES];
}
#pragma mark -
#pragma mark Actions
- (void)takePhotoFunction {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.view.frame = self.photoView.bounds;
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 40, 70, 70);
[self.takePicture addTarget:self action:#selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:#"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto {
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender {
[self.picker takePicture];
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
[self.photoView setImage:imageToSave];
} else {
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker {
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
#end
Solved the issue of the "smaller image" on the PhotoView after taking a picture by cropping it as a square and just after, set the image of the PhotoView.
Solved the issue of the image "going up" a little bit after setting the image on PhotoView by 'setting' the picker.view frame on x: 0, y:-50, PhotoView.width, PhotoView.height.
Here is the fullcode:
#interface CameraViewController ()
{
BOOL isFromAlbum;
}
#end
#implementation CameraViewController
#synthesize photoView;
#synthesize picker;
#synthesize takePicture;
#synthesize selectPhotoButton;
#synthesize takePhotoButton;
#synthesize switchCameraButton;
#pragma mark -
#pragma mark Initialization & Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
[self initInterface];
}
-(void)initInterface
{
[super addBackButton];
self.view.backgroundColor = [UIColor colorWithRed:026.0f/255.0f green:030.0f/255.0f blue:055.0f/255.0f alpha:1.0f];
self.takePhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePhotoButton.frame = CGRectMake(27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.takePhotoButton.layer.cornerRadius = self.takePhotoButton.frame.size.width / 2.0f;
[self.takePhotoButton setImage:[UIImage imageNamed:#"take-photo-btn"] forState:UIControlStateNormal];
[self.takePhotoButton addTarget:self action:#selector(takePhotoFunction) forControlEvents:UIControlEventTouchUpInside];
//[self.takePhotoButton setBackgroundColor:[UIColor colorWithRed:000.0f/255.0f green:255.0f/255.0f blue:000.0f/255.0f alpha:0.25f]];
[self.view addSubview:self.takePhotoButton];
self.selectPhotoButton = [UIButton buttonWithType:UIButtonTypeSystem];
self.selectPhotoButton.frame = CGRectMake(self.view.frame.size.width/2 + 27.5f, self.view.frame.size.width/2 - 55, 110, 110);
self.selectPhotoButton.layer.cornerRadius = self.selectPhotoButton.frame.size.width / 2.0f;
[self.selectPhotoButton setImage:[UIImage imageNamed:#"pick-photo-btn"] forState:UIControlStateNormal];
[self.selectPhotoButton addTarget:self action:#selector(selectPhoto) forControlEvents:UIControlEventTouchUpInside];
//[self.selectPhotoButton setBackgroundColor:[UIColor colorWithRed:0 green:255/255.0f blue:0 alpha:0.25f]];
[self.view addSubview:self.selectPhotoButton];
self.photoView = [[UIImageView alloc] init];
self.photoView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:000.0f/255.0f blue:000.0f/255.0f alpha:0.25f];
self.photoView.frame = CGRectMake(0, 0, 320, 320);
self.photoView.layer.cornerRadius = self.photoView.frame.size.width / 2;
self.photoView.layer.masksToBounds = YES;
self.photoView.userInteractionEnabled = YES;
}
#pragma mark -
#pragma mark Button's Actions
- (void)takePhotoFunction
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
//self.picker.view.frame = self.photoView.bounds;
self.picker.view.frame = CGRectMake(0, -50, photoView.frame.size.width, photoView.frame.size.height + 50);
[self.photoView addSubview:self.picker.view];
self.takePicture = [UIButton buttonWithType:UIButtonTypeSystem];
self.takePicture.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.size.height - 70, 70, 70);
[self.takePicture addTarget:self action:#selector(takePhotoAction:) forControlEvents:UIControlEventTouchUpInside];
[self.takePicture setBackgroundImage:[UIImage imageNamed:#"take-photo-action"] forState:UIControlStateNormal];
[self.photoView addSubview:self.takePicture];
self.switchCameraButton = [[UIButton alloc] init];
self.switchCameraButton.frame = CGRectMake(self.photoView.frame.size.width/2 - 35, self.photoView.frame.origin.y, 70, 70);
[self.switchCameraButton addTarget:self action:#selector(switchCamera:) forControlEvents:UIControlEventTouchUpInside];
[self.switchCameraButton setBackgroundImage:[UIImage imageNamed:#"reverse-camera-btn"] forState:UIControlStateNormal];
[self.photoView addSubview:self.switchCameraButton];
[self.view addSubview:self.photoView];
isFromAlbum = NO;
}
- (void)selectPhoto
{
self.picker = [[UIImagePickerController alloc] init];
self.picker.delegate = self;
self.picker.allowsEditing = YES;
self.picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
isFromAlbum = YES;
[self presentViewController:self.picker animated:YES completion:NULL];
}
-(IBAction)takePhotoAction:(id)sender
{
[self.picker takePicture];
}
-(IBAction)switchCamera:(id)sender
{
if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
}
else if (picker.cameraDevice == UIImagePickerControllerCameraDeviceRear)
{
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
}
- (UIImage*)rotateUIImage:(UIImage*)sourceImage
{
CGSize size = sourceImage.size;
UIGraphicsBeginImageContext(CGSizeMake(size.height, size.width));
[[UIImage imageWithCGImage:[sourceImage CGImage] scale:1.0 orientation:UIImageOrientationDownMirrored] drawInRect:CGRectMake(0,0,size.height ,size.width)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
#pragma mark -
#pragma mark PickerController Delegate Methods
- (void)imagePickerController:(UIImagePickerController *)myPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
//if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo)
if (isFromAlbum == NO)
{
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage)
{
imageToSave = editedImage;
}
else
{
imageToSave = originalImage;
}
// Save the new image (original or edited) to the Camera Roll
//UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
[self.takePhotoButton removeFromSuperview];
[self.switchCameraButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
[self.picker.view removeFromSuperview];
[self.takePicture removeFromSuperview];
CGFloat minimumSide = fminf(imageToSave.size.width, imageToSave.size.height);
CGFloat finalSquareSize = 640.;
//create new drawing context for right size
CGRect rect = CGRectMake(0, 0, finalSquareSize, finalSquareSize);
CGFloat scalingRatio = 640.0/minimumSide;
UIGraphicsBeginImageContext(rect.size);
//draw
[imageToSave drawInRect:CGRectMake((minimumSide - originalImage.size.width)*scalingRatio/2., (minimumSide - originalImage.size.height)*scalingRatio/2., originalImage.size.width*scalingRatio, originalImage.size.height*scalingRatio)];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// if (picker.cameraDevice == UIImagePickerControllerCameraDeviceFront)
// {
// [self rotateUIImage:croppedImage];
// }
//[self.photoView setImage:imageToSave];
[self.photoView setImage:croppedImage];
}
else
{
[self.takePhotoButton removeFromSuperview];
[self.selectPhotoButton removeFromSuperview];
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.photoView setImage:chosenImage];
[self.view addSubview:self.photoView];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)myPicker
{
//[picker.view removeFromSuperview];
[myPicker dismissViewControllerAnimated:YES completion:NULL];
}
#pragma mark -
#pragma mark NavigationController Methods
-(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
#end

Can't load image taken by 4S but can 5S

My application receive images from a UIImagePickerController and when the image comes from a 5S I can properly load the image in quick view in debug, however if the image comes from a 4S I cannot view it and my application does not properly work.
Here is my code for the UIImagePickerController
- (IBAction)scanButton:(UIButton *)sender
{
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
_imagePicker.sourceType = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ? UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary;
if (_imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//_imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
_imagePicker.showsCameraControls = NO;
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
_imagePicker.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
_imagePicker.cameraViewTransform = scale;
CGRect screenRect = [[UIScreen mainScreen]bounds];
_overlay = [[CameraOverlay alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
[_overlay.shootButton addTarget:self action:#selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
[_overlay.cancelButton addTarget:self action:#selector(cancelPhoto) forControlEvents:UIControlEventTouchUpInside];
[_overlay.flashButton addTarget:self action:#selector(flashButton) forControlEvents:UIControlEventTouchUpInside];
_imagePicker.cameraOverlayView = _overlay;
}
_imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
_imagePicker.allowsEditing = NO;
[self presentViewController:_imagePicker animated:YES completion:NULL];
}
And my didFinish method
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
_image = info[UIImagePickerControllerOriginalImage];
_pickerDismissed = YES;
[self dismissViewControllerAnimated:YES completion:nil];
}
When I view _image from a 5S I can load the image, but from a 4S I cannot.
Does anyone have any idea why this might be happening?
Sounds weird, try a different iPhone 4S

open UIImagePickerController in landscape mode

I want to open iphone saved images in my application. my application working in landscape mode. when I trying to load all saved photo from iphone library using presentModalViewController method, it will open in portrait mode. I want that in landscape mode. here is the code:
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.allowsEditing = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
[picker setNavigationBarHidden:TRUE];
[self presentModalViewController:picker animated:NO];
NSLog(#"%#", self.view.subviews);
[picker release];
can any one help me..
Thanks in advance.
You can't. Quoth the documentation:
The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing.
It's a bit more work, but you can use the AssetsLibrary framework to access the list of images and create your own image picker.
we cant get UIImagePickerController in landscape...
but we can get the images in our device into an array and display them in landscape mode and in portrait mode....itseems same like UIImagePickerController...
we should use ALAsset class and ALAssetsLibrary for this..
void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != NULL)
{
[assets addObject:result];
}
};
void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil)
{
[group enumerateAssetsUsingBlock:assetEnumerator];
}
[self meth];
[activity stopAnimating];
[activity setHidden:YES];
};
assets = [[NSMutableArray alloc] init];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:assetGroupEnumerator
failureBlock: ^(NSError *error) { NSLog(#"Failure");}];
in viewDidLoad
-(void)meth
{
NSLog(#"%i",[assets count]);
if(userOrientation==UIInterfaceOrientationPortrait || userOrientation==UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"haii");
[scrollView removeFromSuperview];
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
scrollView.backgroundColor=[UIColor whiteColor];
NSLog(#"%i",[assets count]);
for (int i = 0; i < [assets count]; i++)
{
imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[imgBtn setFrame:CGRectMake((i%4*80)+2,(i/4*80)+2,75,75)];
imgBtn.tag=i;
[imgBtn addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
ALAsset *asset=[assets objectAtIndex:i];
[imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
[scrollView addSubview:imgBtn];
}
scrollView.contentSize = CGSizeMake(320,(([assets count]/4)+1)*300 );
}
if(userOrientation==UIInterfaceOrientationLandscapeRight || userOrientation==UIInterfaceOrientationLandscapeLeft)
{
[scrollView removeFromSuperview];
scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 480,320)];
for (int i = 0; i < [assets count]; i++)
{
imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[imgBtn setFrame:CGRectMake((i%6*80)+2,(i/6*80)+2,75,75)];
imgBtn.tag=i;
[imgBtn addTarget:self action:#selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
ALAsset *asset=[assets objectAtIndex:i];
[imgBtn setImage:[UIImage imageWithCGImage:[asset thumbnail]] forState:UIControlStateNormal];
[scrollView addSubview:imgBtn];
}
scrollView.contentSize = CGSizeMake(480,(([assets count]/4)+1)*300);
}
[self.view addSubview:scrollView];
}
I have tried the code with some additions
[self.navigationController setNavigationBarHidden:YES];
[self addChildViewController:picker];
[self.view addSubview:picker.view];
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
picker.view.frame = self.view.bounds;
[picker didMoveToParentViewController:self];
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
[picker.view removeFromSuperview];
[picker removeFromParentViewController];
}
(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker.view removeFromSuperview];
[picker removeFromParentViewController];
}
I used following code:
[self.view addSubview:imgPicker.view];
[imgPicker viewWillAppear:YES];
[imgPicker viewDidAppear:YES];
instead of using presentModalViewController method,
[self presentModalViewController:picker animated:NO];
I tried this approach, while following the API docs more closely:
- (void)openEmbeddedImagePicker:(UIImagePickerController *)picker {
[self.navigationController setNavigationBarHidden:YES];
[self addChildViewController:picker];
[self.view addSubview:picker.view];
picker.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
picker.view.frame = self.view.bounds;
[picker didMoveToParentViewController:self];
}
But the video recorder's Cancel and playback buttons don't respond. Also, while the containing VC (self in the above method) restricts the orientation to landscape, rotating between the two landscape modes messes up the layout of the picker's overlay bar (on iOS 6, at least).
Does anyone have success restricting the video recorder to landscape mode?
From the UIImagePickerController Class Reference.
Important: The UIImagePickerController class supports portrait mode only. This class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified, with one exception. You can assign a custom view to the cameraOverlayView property and use that view to present additional information or manage the interactions between the camera interface and your code.

Scrolling UIScrollView on external screen attached to an iPad

Hey. I've achieved making a programmatic UIScrollView with zooming, but now I've been trying to take the scrollable/zoomable image to an external screen if plugged in.
#implementation MapVC
UIScrollView *mapScrollView;
UIImageView *mapImageView;
UIImageView *mapImageViewEx;
CGFloat lastScale = 0;
NSMutableArray *map_List;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];
map_List = [[NSMutableArray alloc] init];
[map_List addObject:#"Pacific_Map.png"];
[map_List addObject:#"Atlantic_Map.png"];
CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);
mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];
mapScrollView.backgroundColor = [UIColor blackColor];
[mapScrollView setDelegate:(id<UIScrollViewDelegate>)self];
mapScrollView.contentSize = CGSizeMake(2437, 1536);
mapScrollView.bounces = NO;
mapScrollView.bouncesZoom = NO;
mapScrollView.minimumZoomScale = .5;
mapScrollView.maximumZoomScale = 1.5;
[mapScrollView setZoomScale:mapScrollView.minimumZoomScale];
UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageView = [[UIImageView alloc] initWithImage: mapImage];
[mapImage release];
if(exScreenEnabled==1){
UIImage *mapImageEx = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageViewEx = [[UIImageView alloc] initWithImage: mapImageEx];
[mapImageEx release];
UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];
[containerExViewP addSubview:mapImageViewEx];
}else{
[mapScrollView addSubview:mapImageView];
}
[self addSubview:mapScrollView];
mapImageView.userInteractionEnabled = YES;
UIImage *footerMapIMG = [UIImage imageNamed:#"footer_map_alternate.png"];
UIImageView *footerMapView = [[UIImageView alloc] initWithImage:(UIImage *)footerMapIMG];
CGRect footerMapFrame = CGRectMake(0, 686, 213, 82);
footerMapView.frame = footerMapFrame;
[self addSubview:footerMapView];
footerMapView.image = footerMapIMG;
[footerMapView release];
CGRect backBTNFrame = CGRectMake(20, 714, 140, 52);
UIButton *MAP_backButton = [[UIButton alloc] init];
MAP_backButton.frame = backBTNFrame;
UIImage *MAP_backButtonIMG = [UIImage imageNamed:#"button_back.png"];
[MAP_backButton setImage:MAP_backButtonIMG forState:UIControlStateNormal];
MAP_backButton.backgroundColor = [UIColor clearColor];
[self addSubview:MAP_backButton];
[MAP_backButton release];
[MAP_backButton addTarget:del.switchVC
action:#selector(gotoMapAndListChooser)
forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if(exScreenEnabled==1){
return mapImageViewEx;
}else{
return mapImageView;
}
}
(Sorry I've had no luck getting that formatted to look right on this site)
If a video cable is plugged into an iPad, there's no image on the iPad, which is what I want. The image on the external screen zooms correctly when you do the gesture on the iPad, but I can't figure out how to make it scroll. Thanks in advance.
edit: I now have this -
#implementation MapVC
UIScrollView *mapScrollView;
UIImageView *mapImageView;
UIImageView *mapImageViewEx;
CGFloat lastScale = 0;
NSMutableArray *map_List;
int touchesNum = 0;
-(void)touchesBegan:(NSSet *)theTouches withEvent:(UIEvent *)event {
NSSet *touches = [event allTouches];
touchesNum=[touches count];
NSLog(#"number of touches %i", touchesNum);
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];
map_List = [[NSMutableArray alloc] init];
[map_List addObject:#"Pacific_Map.png"];
[map_List addObject:#"Atlantic_Map.png"];
CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);
mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];
mapScrollView.backgroundColor = [UIColor blackColor];
[mapScrollView setDelegate:(id<UIScrollViewDelegate>)self];
mapScrollView.contentSize = CGSizeMake(2437, 1536);
mapScrollView.bounces = NO;
mapScrollView.bouncesZoom = NO;
mapScrollView.minimumZoomScale = .5;
mapScrollView.maximumZoomScale = 1.5;
[mapScrollView setZoomScale:mapScrollView.minimumZoomScale];
UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageView = [[UIImageView alloc] initWithImage: mapImage];
[mapImage release];
if(exScreenEnabled==1){
UIImage *mapImageEx = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageViewEx = [[UIImageView alloc] initWithImage: mapImageEx];
[mapImageEx release];
UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];
[containerExViewP addSubview:mapImageViewEx];
}else{
[mapScrollView addSubview:mapImageView];
}
[self addSubview:mapScrollView];
mapImageView.userInteractionEnabled = YES;
UIImage *footerMapIMG = [UIImage imageNamed:#"footer_map_alternate.png"];
UIImageView *footerMapView = [[UIImageView alloc] initWithImage:(UIImage *)footerMapIMG];
CGRect footerMapFrame = CGRectMake(0, 686, 213, 82);
footerMapView.frame = footerMapFrame;
[self addSubview:footerMapView];
footerMapView.image = footerMapIMG;
[footerMapView release];
CGRect backBTNFrame = CGRectMake(20, 714, 140, 52);
UIButton *MAP_backButton = [[UIButton alloc] init];
MAP_backButton.frame = backBTNFrame;
UIImage *MAP_backButtonIMG = [UIImage imageNamed:#"button_back.png"];
[MAP_backButton setImage:MAP_backButtonIMG forState:UIControlStateNormal];
MAP_backButton.backgroundColor = [UIColor clearColor];
[self addSubview:MAP_backButton];
[MAP_backButton release];
[MAP_backButton addTarget:del.switchVC
action:#selector(gotoMapAndListChooser)
forControlEvents:UIControlEventTouchUpInside];
mapScrollView.multipleTouchEnabled = YES;
}
return self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
if(exScreenEnabled==1){
return mapImageViewEx;
}else{
return mapImageView;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)inscrollView{
if(touchesNum==0){
CGPoint p = mapScrollView.contentOffset;
mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageViewEx.frame.size.width, mapImageViewEx.frame.size.height);
}
}
- (void)dealloc {
[mapScrollView release];
[mapImageView release];
[map_List release];
[super dealloc];
}
#end
As I said below, I can now get either scroll or zooming to work separately, but zooming is all messed up if scrolling is working, because when zooming it thinks it's also scrolling. So I want to avoid it scrolling when zooming, and to do this I want to detect the number of touches, which I must be doing wrong!
Got it working with the image being on the iPad and external screen. I'll probably swap it in with a rectangular area because the image is resource heavy to be both the iPad and external screen.
#import "exGlobal.h"
#import "mapVC.h"
#import "switchVC.h"
#import "switchExVC.h"
#import "mainMenuAppDelegate.h"
#import <MobileCoreServices/MobileCoreServices.h>
#implementation MapVC
UIScrollView *mapScrollView;
UIImageView *mapImageView;
UIImageView *mapImageViewEx;
CGFloat lastScale = 0;
NSMutableArray *map_List;
static int toggleScroll = 1;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];
map_List = [[NSMutableArray alloc] init];
[map_List addObject:#"Pacific_Map.png"];
[map_List addObject:#"Atlantic_Map.png"];
CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);
mapScrollView = [[UIScrollView alloc] initWithFrame:mapScrollViewFrame];
mapScrollView.backgroundColor = [UIColor blackColor];
[mapScrollView setDelegate:(id<UIScrollViewDelegate>)self];
mapScrollView.contentSize = CGSizeMake(2437, 1536);
mapScrollView.bounces = NO;
mapScrollView.bouncesZoom = NO;
mapScrollView.minimumZoomScale = .5;
mapScrollView.maximumZoomScale = 1.5;
[mapScrollView setZoomScale:mapScrollView.minimumZoomScale];
UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageView = [[UIImageView alloc] initWithImage: mapImage];
[mapImage release];
if(exScreenEnabled==1){
UIImage *mapImageEx = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];
mapImageViewEx = [[UIImageView alloc] initWithImage: mapImageEx];
[mapImageEx release];
UIView *containerExViewP = (UIView*)[del.switchExVC.view viewWithTag:9000];
[containerExViewP addSubview:mapImageViewEx];
[mapScrollView addSubview:mapImageView]; // see if this works ok on iPad. Map on TV AND iPad.
}else{
[mapScrollView addSubview:mapImageView];
}
[self addSubview:mapScrollView];
mapImageView.userInteractionEnabled = YES;
UIImage *footerMapIMG = [UIImage imageNamed:#"footer_map_alternate.png"];
UIImageView *footerMapView = [[UIImageView alloc] initWithImage:(UIImage *)footerMapIMG];
CGRect footerMapFrame = CGRectMake(0, 686, 213, 82);
footerMapView.frame = footerMapFrame;
[self addSubview:footerMapView];
footerMapView.image = footerMapIMG;
[footerMapView release];
CGRect backBTNFrame = CGRectMake(20, 714, 140, 52);
UIButton *MAP_backButton = [[UIButton alloc] init];
MAP_backButton.frame = backBTNFrame;
UIImage *MAP_backButtonIMG = [UIImage imageNamed:#"button_back.png"];
[MAP_backButton setImage:MAP_backButtonIMG forState:UIControlStateNormal];
MAP_backButton.backgroundColor = [UIColor clearColor];
[self addSubview:MAP_backButton];
[MAP_backButton release];
[MAP_backButton addTarget:del.switchVC
action:#selector(gotoMapAndListChooser)
forControlEvents:UIControlEventTouchUpInside];
mapScrollView.multipleTouchEnabled = YES;
}
return self;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return mapImageView;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
if(exScreenEnabled==1){
CGPoint p = mapScrollView.contentOffset;
mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageView.frame.size.width, mapImageView.frame.size.height);
}
}
- (void)scrollViewDidScroll:(UIScrollView *)inscrollView{
if(exScreenEnabled==1 && toggleScroll==1){
CGPoint p = mapScrollView.contentOffset;
mapImageViewEx.frame = CGRectMake((p.x*-1), (p.y*-1), mapImageView.frame.size.width, mapImageView.frame.size.height);
}
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)theScrollView withView:(UIView *)view{
NSLog(#"BEGIN ZOOMING");
toggleScroll=0;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)theScrollView withView:(UIView *)view atScale:(float)scale{
NSLog(#"END ZOOMING");
toggleScroll=1;
}
- (void)dealloc {
[mapScrollView release];
[mapImageView release];
[map_List release];
[super dealloc];
}
#end
I pressed the space bar (4) times for code, and NOPE doesn't work stack overflow still broken. :)

Resources