In my application I m using UIImagePickerControllerCropRect to crop the image but I want to reduce the size of that cropping box of iphone which comes automatically so that my image crop to its perfect size which I want. I'm using the following code for that:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
picker.allowsImageEditing = YES;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = image;
CGSize size = [imageView.image size];
CGRect cropRect = CGRectMake(0.0, 0.0, size.width, size.height);
NSLog(#"Original image size = (%f, %f)", size.width, size.height);
NSValue *cropRectValue = [editingInfo objectForKey:#"UIImagePickerControllerCropRect"];
cropRect = [cropRectValue CGRectValue];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, nil, nil);
}
Related
So in my iPhone 4 device after I pick a image I want the image picker popover to go away. This works in iPhone 4 but the following code doesn't work for the iPhone 5.
- (void) loadImage:(UIImage*) image {
float w = image.size.width;
float h = image.size.height;
float maxw = scrollView.frame.size.width;
float maxh = scrollView.frame.size.height;
float wratio = maxw / w;
float hratio = maxh / h;
float ratio = wratio < hratio ? wratio : hratio;
ratio = ratio < 1 ? ratio : 1;
int adjW = (int) (w * ratio);
int adjH = (int) (h * ratio);
UIGraphicsBeginImageContext(CGSizeMake(adjW, adjH));
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect:CGRectMake(0, 0, adjW, adjH)];
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
UIGraphicsEndImageContext();
[appDelegate.model setCurrentImage:[UIImage imageWithCGImage: scaledImage]];
[self clear];
calcButton.enabled = YES;
trashButton.enabled = YES;
scribbleControls.enabled = YES;
[appDelegate.mergeViewCtlr setFirst];
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self loadImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
if ([popoverController isPopoverVisible]) {
// called for iPad
[popoverController dismissPopoverAnimated:YES];
}
}
else {
// called for iPhone and tried each of the next 3 lines individually
[self dismissModalViewControllerAnimated:YES]; <== NOT WORKING
[self dismissViewControllerAnimated:YES completion:nil]; <== ALSO NOT WORKING
[picker dismissViewControllerAnimated:YES completion:nil]; <== ALSO NOT WORKING
}
[picker release];
}
I also noticed that it said dimissModelViewControllerAnimated has been deprecated and that instead I should use:
Use dismissViewControllerAnimated:completion: instead. But how would I use this? Thanks
Here is the code that presents the model view:
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: { //photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ) {
if ([popoverController isPopoverVisible]) {
[popoverController dismissPopoverAnimated:YES];
}
else {
popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
popoverController.delegate = self;
[popoverController presentPopoverFromRect:CGRectMake( 250, -50, 320, 480 )
inView:[self view]
permittedArrowDirections:UIPopoverArrowDirectionUp
animated:YES];
}
}
else { // for iPhone
[self presentModalViewController:imagePicker animated:TRUE];
}
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Photo library is empty or unavailable" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
break;
}
case 1: //camera
dismissModalViewControllerAnimated:: was deprecated in iOS6. Use dismissViewControllerAnimated:completion: instead.
[self dismissViewControllerAnimated:YES completion:nil];
presentModalViewController, and dismissModalViewController depracated in iOS6
Try this,
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
and use
[self presentViewController:self.picker animated:YES completion:nil];
You can also use respondsToSelector to make the cases for iOS 5 and ios6.
I'm trying to get image from gallery and than display it in UIImageView. My problem is in displaying this image. My code:
- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.popController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
self.popController.delegate = self;
[self.popController presentPopoverFromRect:[fromGalaryButton frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[self.popController dismissPopoverAnimated:YES];
self.image = [info objectForKey:UIImagePickerControllerEditedImage];
[self updateDisplay];
}
-(void)updateDisplay {
self.imageView.image = self.image;
self.imageView.hidden = NO;
[self.imageView reloadInputViews];
}
My self.imageView is displaying normally. But there is no any image in. There is my problem?
If you are just picking in image from the gallery then the line of code in didFinishPickingMediaWithInfo method should be:
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
You could also just update the imageView from within the method as such:
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
This is how I did this:
- (void)viewDidLoad {
[super viewDidLoad];
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
imagePickerPopover = [[UIPopoverController alloc] initWithContentViewController:picker];
}
- (IBAction)getPhoto:(id)sender {
if(sender == choosePhotoButton) {
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[imagePickerPopover presentPopoverFromBarButtonItem:choosePhotoButton permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[imagePickerPopover dismissPopoverAnimated:YES];
imageViewer.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
i am working on a camera app. i want to restrict the image content in particular dimensions, but the image through camera is coming in full screen. How can i minimize it?
- (void) viewDidAppear:(BOOL)animated {
OvlayView *overlay = [[OvlayView alloc] initWithFrame:CGRectMake(472, 158, 240, 128)];
// Create a new image picker instance:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
// Set the image picker source:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
// Hide the controls:
picker.showsCameraControls = YES;
picker.navigationBarHidden = YES;
// Make camera view full screen:
picker.wantsFullScreenLayout = NO;
picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
// Insert the overlay:
picker.cameraOverlayView = overlay;
/*UIGraphicsBeginImageContext(CGSizeMake(480,320));
CGContextRef context = UIGraphicsGetCurrentContext();
[image drawInRect: CGRectMake(0, 0, 480, 320)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();*/
// Show the picker:
[self presentModalViewController:picker animated:YES];
[super viewDidAppear:YES];
}
Facing some weird moment of my career. I have some uitextfield's and one uitextview in my UIViewController....everything works fine on simulator and iPhone 4 iOS v 4.3.5. But in iPhone 4 iOS v 4.0.1 sometimes I get whole view refreshed. That meant sometimes my uitextfields & uitextview lost text after picking image from camera.....
for solving this my attempt was when view disappear I saved values in some array..... and when view did appear i show array values to textboxes.....its works fine with simulator too. but this time my apps get stucked on iPhone 4 iOS v 4.0.1.
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(#"title : %#",actionSheet.title);
if (![actionSheet.title isEqualToString:#"Success"] && ![actionSheet.title isEqualToString:#"Error"] && ![actionSheet.title isEqualToString:#"خطأ"]) {
if(buttonIndex == 1) {
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
} else {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController: picker animated:YES];
[picker release];
}
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
float actualHeight = img.size.height;
float actualWidth = img.size.width;
float imgRatio = actualWidth/actualHeight;
float maxRatio = 320.0/480.0;
if(imgRatio!=maxRatio){
if(imgRatio < maxRatio){
imgRatio = 480.0 / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = 480.0;
}
else{
imgRatio = 320.0 / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = 320.0;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[img drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
if(imageCount == 1){
self.firstImage = image;
self.previewImage1.image =firstImage;
}else if(imageCount == 2){
self.secondImage = image;
self.previewImage2.image =secondImage;
}else if(imageCount == 3){
self.thirdImage = image;
self.previewImage3.image =thirdImage;
}else if(imageCount == 4){
self.fourthImage = image;
self.previewImage4.image =fourthImage;
}
[myImageNames replaceObjectAtIndex:imageCount-1 withObject:#"Assigned"];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
Solved..Deleted app from device. then restarted device and install build again....
Now no problem with uitextfield refresh on picking image from camera..
chill guys....:-)
I have a uibutton to trigger an iphone's camera to grab an image, and I have a uiimageView in same view to show captured image, but it not showing anything after capturing.
Any solution?
here's my code:
(IBAction)grabImage:(id)sender{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController: picker animated:YES];
[picker release];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage : (UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
cameraImageView.image = image;
[picker dismissModalViewControllerAnimated:YES];
}