Objective-C: Use the Original Image Size After Capture - objective-c

I am making an app that uses the camera.
Everything is working well, but when i started to add the captured image on a UIImageView it resizes automatically with respect to the size of UIImageView.
What i want it to do is, use the original size of the captured image.
Here's my code:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[insertPhoto1 setImage:image];
[self dismissModalViewControllerAnimated:YES];
}

The problem is with the contentMode of the imageView:
insertPhoto1.contentMode=UIViewContentModeCenter;

Related

iOS full screen camera overlay

I am new to objective-c and have been searching for an answer to this question for a while...
I am creating a photo-sharing app and I want the camera to be full-screen. I have used the following code to hide the default camera overlay and create my own, and make the camera full-screen. On the next screen, I want to display the image captured as full-screen again, but the image is getting collapsed sideways.
Setting the camera overlay:
-(void)loadCamera {
self.imagePicker = [[UIImagePickerController alloc]init];
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imagePicker.delegate = self;
self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
self.imagePicker.showsCameraControls = NO;
self.overlayIV = [[WAUCameraOverlayView alloc]initWithFrame:self.imagePicker.cameraOverlayView.frame];
[self.imagePicker.cameraOverlayView addSubview:self.overlayIV];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
self.imagePicker.cameraViewTransform = translate;
CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
self.imagePicker.cameraViewTransform = scale;
[self presentViewController:self.imagePicker animated:YES completion:nil];
}
The above code works great and makes the camera full-screen with my custom overlay. Then, this block of code gets executed:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self.overlayIV removeFromSuperview];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
I pass "image" to the next screen and initialize it to the size of the screen, but it gets stretched... any way to save/display the image captured from the full-screen camera in the exact ratio and size?
EDIT: The code I use to display the image is:
UIImageView *capturedImageView =[[UIImageView alloc]initWithFrame:self.frame];
capturedImageView.image = aImage;
capturedImageView.contentMode = UIViewContentModeScaleAspectFill;
[self addSubview:capturedImageView];
I want the image to be the size of the screen, and the same ratio/aspect as it was captured in the camera

UIImageView Won't Show When ViewController Added As Subview

My main aim is to have one background for all of my ViewControllers. Every ViewController I have has a clear background.
To do this, I have made one UIViewController (called backgroundViewController) that will act as the subview to all my other ViewControllers. It has one UIImageView which displays this particular background. I will then add this backgroundViewController as a subview of all my other ViewControllers.
The problem is - this imageView won't show as a subview!
This is how I display the imageView:
- (void)viewDidLoad
{
if ([musicPlayer playbackState] == MPMusicPlaybackStateStopped) {
UIImage *artworkBackgroundView;
artworkBackgroundView = [UIImage imageNamed:#"noArtworkBackground"];
UIImage *effectImage = nil;
backgroundView.image = effectImage;
[backgroundView setImage:artworkBackgroundView];
backgroundView.image = effectImage;
}
}
- (void) handle_NowPlayingItemChanged: (id) notification
{
if ([musicPlayer playbackState] != MPMusicPlaybackStateStopped) {
// Get artwork for current now playing item
MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
UIImage *artworkBackgroundView = [artwork imageWithSize: CGSizeMake(618, 618)];
if (!artworkImage) {
artworkBackgroundView = [UIImage imageNamed:#"noArtworkBackground"];
artworkImage = [UIImage imageNamed:#"noArtwork"];
}
[backgroundView setImage:artworkBackgroundView];
}
}
As you can see, backgroundView changes each time the music player skips song.
To test that backgroundViewController does show as a subview, I added another imageView and changed its image to a static .png in Interface Builder, and that shows correctly as a subview.
This is how I make it a subview for other ViewControllers:
backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:#"backgroundViewController" bundle:nil];
[self.view insertSubview:backgroundVC.view atIndex:0];
What I want is the UIImageView called backgroundView to show up when it is being called as subview.
I have tested that backgroundView does change according to what song is playing, and it works correctly.
What am I doing wrong? backgroundView refuses to show up as a subview?! I've searched a ton about adding ViewControllers as subviews but I can't find a similar problem.
Any help would be much appreciated, thanks! :)
This worked for me:
backgroundViewController *backgroundVC = [[backgroundViewController alloc] initWithNibName:#"backgroundViewController" bundle:nil];
[self addChildViewController:backgroundVC];
[self.view addSubview:backgroundVC.view];
I found the solution here. I need to read more about 'Custom Containers'.

Bug on submitted app binary but not in the simulator - CALayer position contains NaN

I submitted my app to the App Store where is ready to download. I've since then received some interesting crash reports when people select an image from the ImagePicker in one of my views.
This bug (see below) makes the app crash. I was wondering 2 things.
Can anyone spot the problem in the code below?
How do you deal with bugs that are only in the App Binary but do not show up when trying to recreate them on the dev environment? - I can make the app crash with the Binary that is on the app store but when I do the same on the simulator or on my test phone the app works perfectly..
The Crash report in BugSense
CALayer position contains NaN: [798 nan]
Class:
CALayerInvalidGeometry
0x00120e99 -[imageCroppingViewController imagePickerController:didFinishPickingMediaWithInfo:] (imageCroppingViewController.m:126) + 163481
The Code
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
CGRect rect;
rect.size.width = image.size.width;
rect.size.height = image.size.height;
imageView.center = scrollView.center;
[imageView setFrame:rect];
scrollView.contentSize = imageView.frame.size;
self.navigationController.navigationBar.hidden = NO;
[myPicker.view removeFromSuperview];
}
You do not initialize completely your local variable rect. Its origin member is undefined and may be anything, when you send [imageView setFrame:rect];.
Add rect.origin = CGPointZero; or use CGRectMake to initialize rect :
rect = CGRectMake(0,0,image.size.width,image.size.height);
Or any location you want for your rect.

Subclassing UIActivityIndicator to change the Image

This is what I did:
#implementation BGUIActivityIndicator
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self customInitialize];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
[self customInitialize];
return self;
}
-(void)customInitialize
{
UIView * theImageView= [self findASubViewforClass:[UIImageView class]];//
UIImageView * theImageView1 = (UIImageView *) theImageView;
theImageView1.image = [UIImage imageNamed:#"spinner_blue"];
[theImageView1.image saveScreenshot];
while (false) ;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Everything seems perfect. [theImageView1.image saveScreenshot]; jot down both the old and new view perfectly.
However, nothing changes. Why?
I am not exactly asking how to change the image of UIActivityIndicator. There are tons of it already. I want to use it by subclassing UIActivityIndicator because I think it's the most elegant solution. I can't seem to do that.
In particular, I am asking why my approach, which works for changing background of search controller, for example, doesn't work for this.
According to the UIActivityIndicatorView Class Reference ,there is no way/ chance to change the image through sub-classing.
However you can change its activityIndicatorViewStyle , color of the activity indicator,UIActivityIndicatorStyle etc..
I think, without sub-classing, the class class UIImageView provides a very useful and simple way to implement such a thing. The only thing you have to do is to:
1.Provide a number of images that reflect your indicator animation.
2.Create a new UIImageView instance and set images and animation duration.
3.Position your custom activity indicator within your current view.
SAMPLE CODE:
//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:#"status1.png"];
UIImageView *activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"status1.png"],
[UIImage imageNamed:#"status2.png"],
[UIImage imageNamed:#"status3.png"],
[UIImage imageNamed:#"status4.png"],
[UIImage imageNamed:#"status5.png"],
[UIImage imageNamed:#"status6.png"],
[UIImage imageNamed:#"status7.png"],
[UIImage imageNamed:#"status8.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 0.8;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectMake(
self.view.frame.size.width/2
-statusImage.size.width/2,
self.view.frame.size.height/2
-statusImage.size.height/2,
statusImage.size.width,
statusImage.size.height);
//Start the animation
[activityImageView startAnimating];
//Add your custom activity indicator to your current view
[self.view addSubview:activityImageView];
See the full details Here

UIImagePickerController: invoke takePicture without shuttering animation

I'm invoking UIImagePickerController's method takePicture method to programmatically pictures with the iPhone
However I would like to disable the shuttering animation, and just leave the screen as it is, when the picture is taken (and disabling the sound as well).
UPDATE
I'm now using AVFoundation APIs to get the image, but the content of the camera is not displayed anymore on the screen. Should I initialize a UIImagePickerController just to display the content of the camera on the screen ?
Thanks
Check out this sample: It uses AVCaptureSession and an ImagePicker to capture images from the video.
http://developer.apple.com/library/ios/#samplecode/AVCam/Listings/Classes_AVCamCaptureManager_m.html
Also this one: https://github.com/jj0b/AROverlayExample
UIImagePickerController has some options, but not a lot. This other question has some tacky but probably effective ideas for disabling it.
For complete control, you need to use the AVFoundation APIs. Here is a capture example: https://developer.apple.com/library/ios/#samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112
I think this useful for 3.0
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"])
{
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage * cameraimage= [self scaleAndRotateImage:selectedImage];
CGRect rect =CGRectMake(0, 0,640,875);
CGImageRef imageRef = CGImageCreateWithImageInRect([selectedImage CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
self.captureimage.image=cameraimage;
NSLog(#"selectedimage width:%f ht:%f",img.size.width,img.size.height);
}
[picker dismissModalViewControllerAnimated:YES];
}