UIProgressView when putting image into UIImageView - iphone

I wonder, how could I display UIProgressView in this case:
I do photo with UIImagePickerController then in method - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
I save the picture to UIImage object (outputImage) and hide controller, and when modalView/controller with camera is disappeared I have such code:
NSLog(#"Image is width: %f height: %f", outputImage.size.width, outputImage.size.height);
//FLIP PICTURE FROM FACETIME CAMERA
if((outputImage.size.width == 480 && outputImage.size.height == 640) || (outputImage.size.width == 640 && outputImage.size.height == 480)) {
outputImage = [UIImage imageWithCGImage:outputImage.CGImage
scale:1.0 orientation: UIImageOrientationLeftMirrored];}
//RESIZE TO 1000x1000
outputImage = [self scaleAndCropToSize:outputImage toSize:CGSizeMake(1000, 1000)];
NSLog(#"Now image is width: %f height: %f", outputImage.size.width, outputImage.size.height);
//CUTING OUT TO MASK
outputImage = [self maskImage:outputImage withMask:[UIImage imageNamed:#"mask.png"]];
//ADDING SHADOW
CGSize size = CGSizeMake(1000, 1000);
UIGraphicsBeginImageContext(size);
CGPoint thumbPoint = CGPointMake(0, 0);
[outputImage drawAtPoint:thumbPoint];
UIImage* shadow = [UIImage imageNamed:#"shadow.png"];
CGPoint starredPoint = CGPointMake(0, 0);
[shadow drawAtPoint:starredPoint];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
outputImage = result;
//ADDING TO UIIMAGEVIEW (PREVIEW)
preview.image = outputImage;
While photo is being put into UIImageView, it takes some time. In this time I'd like to display progress view. How can I do that?
EDIT: I thought to place above code in some method and then somehow implement progressview, that would base on method completion time. But dunno.

You can not use UIProgressView as its difficult to know how much time it will take to perform image operations.
you can try this MBProgressHUD
Put this code in didFinishPickingMediaWithInfo
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.labelText = #"Saving Image...";
[self.view addSubview:HUD];
HUD.delegate = self;
[HUD showWhileExecuting:#selector(savingPicture:) onTarget:self withObject:selectedImage animated:YES];
and put your resize/crop image operations code in savingPicture method.

Progress views are more appropriate when you are running a loop and you know what percentage of the work is being completed, to show the progress in the progress bar. In this case you can show a UIActivityIndicator instead, until your image is ready to be put on the image view.

Related

Capture still image be stretched with Cocoa Touch AVFoundation framework

I must capture still image with cocoa touch AVFoundation framework,but I found that captured image be stretched.
I have configured the captureSession as these:
[self setPreviewLayer:[[AVCaptureVideoPreviewLayer alloc] initWithSession:[self captureSession]]];
self.captureSession.sessionPreset=AVCaptureSessionPresetHigh;
[[self previewLayer] setVideoGravity:AVLayerVideoGravityResizeAspectFill];
And use these code to capture image:
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
if(error)
NSLog(#"error=%#",error);
else
{
if (imageDataSampleBuffer != NULL) {
NSData *imageData=[AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image=[[UIImage alloc] initWithData:imageData];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
CGSize screenSize=CGSizeMake(screenWidth, screenHeight);
NSLog(#"screen.width=%f,screen.height=%f",screenWidth,screenHeight);
UIGraphicsBeginImageContextWithOptions(screenSize, NO, 0.0f);
// This is where we resize captured image
[(UIImage *)image drawInRect:CGRectMake(0, 0, screenWidth, screenHeight)];
CGSize imageSize=[ImageUtility getImageSize:image];
NSLog(#"image.width=%f,image.height=%f",imageSize.width,imageSize.height);
// Save the results directly to the image view property
UIImage *toSaveImage=UIGraphicsGetImageFromCurrentImageContext();
imageSize=[ImageUtility getImageSize:toSaveImage];
NSLog(#"combine.width=%f,combine.height=%f",imageSize.width,imageSize.height);
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(toSaveImage, nil, nil, nil);
}
}
}];
}
This result output:
013-08-29 09:30:06.182 WatermarkCamera[6882:907] screen.width=320.000000,screen.height=480.000000
2013-08-29 09:30:07.208 WatermarkCamera[6882:907] image.width=1280.000000,image.height=720.000000
2013-08-29 09:30:07.238 WatermarkCamera[6882:907] combine.width=640.000000,combine.height=960.000000
The result output image stretched.How to fix the stretched image?Any suggestions should be appreciated.
I think I shouldn't draw image with the size of screen.I should draw image with the size of image.
I have defined new method to scale image to new width
+(UIImage*)imageWithImage: (UIImage*) sourceImage scaledToWidth: (float) i_width;
Now I called this method
UIImage *newImage=[ImageUtility imageWithImage:image scaledToWidth:screenWidth];
[newImage drawInRect:CGRectMake(0, 0, newImage.size.width, newImage.size.height)];
Finally write the new image to photo album.The new image doesn't stretch.

crop image in circular shape

I am creating an application in which i am displaying one .jpg image. I want to crop part of image in circular shape. Please help me to solve this problem.
image = [UIImage imageNamed:#"images2.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
CGSize size = [image size];
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];
Please tell me to crop part of image in circular shape
You can do it by using Quartz Core framework it really has some cool Apis to do that. Check this RoundedImageView example.
Methinks this is a duplicate. There's an excellent accepted answer in this question, and links to other articles: How to crop UIImage on oval shape or circle shape?
EDIT: There's a handful of easy ways of going about this. A CALayer with a cornerRadius being obvious. But more importantly, there exists the method CGImageCreateWithMask: which can be applied to a broader spectrum of up to and including circles and other shapes. Note that if your image is a JPEG, then CGImageCreateWithMask will return a black background because JPEG's have no alpha channel.
You can use RSKImageCropper for crop the image in circular shape. I am implemented the fallowing code to crop the image in circular shape with the help of RSKImageCropper.
1. Install the pod RSKImageCropper.
2. #import <RSKImageCropper/RSKImageCropper.h> in your viewcontroller
3. Add delegate to your interface i.e. RSKImageCropViewControllerDelegate
4. Implement the fallowing code in **didFinishPickingMediaWithInfo** delegate.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:
^{
RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:originalImage];
imageCropVC.avoidEmptySpaceAroundImage = YES;
imageCropVC.delegate = self;
[self presentViewController:imageCropVC animated:NO completion:nil];
}];
}
5. Now implement the delegate of RSKImageCropper.
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
{
[controller dismissViewControllerAnimated:NO completion:nil];
}
// The original image has been cropped.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
{
self.imgVIew.image = croppedImage;
[self.navigationController popViewControllerAnimated:YES];
}
// The original image has been cropped. Additionally provides a rotation angle used to produce image.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
rotationAngle:(CGFloat)rotationAngle
{
self.imgVIew.image = croppedImage;
[controller dismissViewControllerAnimated:NO completion:nil];
}
For more info check this https://github.com/ruslanskorb/RSKImageCropper

iPhone : Image Captured in portrait mode is loaded in landscape mode in UIImageview

I am developing an Application where I am capturing image in portrait mode and then moving to another class with Image data and showing that image in a UIImageView on a UIScrollView.
Now If I am taking image form library,it is working fine, but If I have to capture an Image it is showing data in landscape mode. Below is my code of both classes. :
Class 1:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *imgData = UIImagePNGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"]);
[picker dismissModalViewControllerAnimated:YES];
image = [[UIImage alloc] initWithData:imgData];
CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop];
PhotoCropperPage *photoCropper = [[PhotoCropperPage alloc]
initWithPhoto:photo
delegate:self
uiMode:PCPUIModePresentedAsModalViewController
showsInfoButton:YES];
[photoCropper setMinZoomScale:0.3f];
[photoCropper setMaxZoomScale:4.0f];
[self.navigationController pushViewController:photoCropper animated:YES];
}
Class 2:
- (void) loadPhoto
{
if (self.photo == nil) {
return;
}
CGFloat w = self.photo.size.width;
CGFloat h = self.photo.size.height;
CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
self.scrollView.contentSize = imageViewFrame.size;
UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
iv.image = self.photo;
[self.scrollView addSubview:iv];
self.imageView = iv;
[iv release];
}
I am not getting where I am doing a mistake? Can anybody point me out? Thanks in advance.
Just found an awesome answer to your question. Here's the link: https://stackoverflow.com/a/5427890/1047258
And then you can do something like that:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *theImage = [[info objectForKey:#"UIImagePickerControllerOriginalImage"]fixOrientation];
//rest of your code
}
and theImage will be your image with correct orientation. It worked in my project
Hope it helps.
be careful not to throw away the orientation data... Try this:
image = [[UIImage alloc] initWithData:imgData];
CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop scale:image.scale orientation:image.orientation ];
also, you'll want to make sure you're cropping the right pixels depending in the image's orientation.

Taking a picture from the camera and show it in a UIImageView

I have a view with some fields (name, price, category) and a segmented control, plus this button to take picture.
If I try this on the simulator (no camera) it works properly: I can select the image from the camera roll, edit it and go back to the view, which will show all the fields with their contents .
But on my iphone, when I select the image after the editing and go back to the view, all the fields are empty exept for the UIImageView.I also tried to save the content of the fields in variables and put them back in the "viewWillApper" method, but the app crashes.
Start to thinking that maybe there is something wrong methods below
EDIT
I found the solution here. I defined a new method to the UIImage class. (follow the link for more information).Then I worked on the frame of the UIImageView to adapt itself to the new dimension, in landscape or portrait.
-(IBAction)takePhoto:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.imgPicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
} else {
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
[self presentModalViewController:self.imgPicker animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
NSDate *date = [NSDate date];
NSString *photoName = [dateFormatter stringFromDate:date];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUs erDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", photoName]];
UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage];
// ---------RESIZE CODE--------- //
if (picture.size.width == 1936) {
picture = [picture scaleToSize:CGSizeMake(480.0f, 720.0f)];
} else {
picture = [picture scaleToSize:CGSizeMake(720.0f, 480.0f)];
}
// --------END RESIZE CODE-------- //
photoPreview.image = picture;
// ---------FRAME CODE--------- //
photoPreview.contentMode = UIViewContentModeScaleAspectFit;
CGRect frame = photoPreview.frame;
if (picture.size.width == 480) {
frame.size.width = 111.3;
frame.size.height =167;
} else {
frame.size.width = 167;
frame.size.height =111.3;
}
photoPreview.frame = frame;
// --------END FRAME CODE-------- //
NSData *webData = UIImagePNGRepresentation(picture);
CGImageRelease([picture CGImage]);
[webData writeToFile:imagePath atomically:YES];
imgPicker = nil;
}
Now I have a new issue! If I take a picture in landscape, and try to take another one in portrait, the app crashs. Do I have to release something?
I had the same issue, there is no edited image when using the camera, you must use the original image :
originalimage = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];
if ([editingInfo objectForKey:UIImagePickerControllerMediaMetadata]) {
// test to chek that the camera was used
// especially I fund out htat you then have to rotate the photo
...
If it was cropped when usign the album you have to re-crop it of course :
if ([editingInfo objectForKey:UIImagePickerControllerCropRect] != nil) {
CGRect cropRect = [[editingInfo objectForKey:UIImagePickerControllerCropRect] CGRectValue];
CGImageRef imageRef = CGImageCreateWithImageInRect([originalimage CGImage], cropRect);
chosenimage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
} else {
chosenimage = originalimage;
}
The croprect info is also present for the camera mode, you need to check how you want it to behave.
To Crop image i think this may help you
UIImage *croppedImage = [self imageByCropping:photo.image toRect:tempview.frame];
CGSize size = CGSizeMake(croppedImage.size.height, croppedImage.size.width);
UIGraphicsBeginImageContext(size);
CGPoint pointImg1 = CGPointMake(0,0);
[croppedImage drawAtPoint:pointImg1 ];
[[UIImage imageNamed:appDelegete.strImage] drawInRect:CGRectMake(0,532, 150,80) ];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
croppedImage = result;
UIImageView *mainImageView = [[UIImageView alloc] initWithImage:croppedImage];
CGRect clippedRect = CGRectMake(0, 0, croppedImage.size.width, croppedImage.size.height);
CGFloat scaleFactor = 0.5;
UIGraphicsBeginImageContext(CGSizeMake(croppedImage.size.width * scaleFactor, croppedImage.size.height * scaleFactor));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextClipToRect(currentContext, clippedRect);
//this will automatically scale any CGImage down/up to the required thumbnail side (length) when the CGImage gets drawn into the context on the next line of code
CGContextScaleCTM(currentContext, scaleFactor, scaleFactor);
[mainImageView.layer renderInContext:currentContext];
appDelegete.appphoto = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Should repeated use of the camera crash an app?

I have an app that builds a slideshow from user images. They can grab from their library or take a picture. I have found that repeated use of grabbing an image from the library is fine. But repeated use of taking a picture causes erratic behavior. I have been getting crashes but mostly what happens seems to be a reloading of the view after "didFinishPickingMediaWithInfo", which messes things up.
I have no leaks and it seems to be releasing properly after each picture is taken. I am resizing the image and saving it in a data base. Is anyone else running into this situation? Was the camera not designed to be called this often?
Sorry for the mix up. It's not continuos, it's take a picture, remove and release the camera, then the user selects choice A, B or C (they are prompted throughout to make selections on many things) then they can take another picture, remove and release the camera...etc...and this happens several times while they complete all the data entry.
After they select the camera I call this code. I am not releasing the image picker in the dealloc method.
- (void)openCamera {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
}
After they "USE" a picture taken from the camera I call the following code. It resized the image based on the original size of the image.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *tempCameraImage = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
[picker dismissModalViewControllerAnimated:YES];
CGFloat originalSize = tempCameraImage.size.width * tempCameraImage.size.height;
NSLog(#"Original Size %f", originalSize);
if (originalSize > 2500000.0) {
CGSize size = tempCameraImage.size;
CGRect rect = CGRectMake(0.0, 0.0, .23 * size.width, .23 * size.height);
UIGraphicsBeginImageContext(rect.size);
[tempCameraImage drawInRect:rect];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGFloat totalSize = theImage.size.width * theImage.size.height;
NSLog(#"Final Camera Size %f", totalSize);
[self resizeImageCamera];
return;
}
else {
CGSize size = tempCameraImage.size;
CGRect rect = CGRectMake(0.0, 0.0, .27 * size.width, .27 * size.height);
UIGraphicsBeginImageContext(rect.size);
[tempCameraImage drawInRect:rect];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGFloat totalSize = theImage.size.width * theImage.size.height;
NSLog(#"Final Camera Size %f", totalSize);
[self resizeImageCamera];
return;
}
}
-(void)resizeImageCamera {
if (editingImage1) {
NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:#"Image" inManagedObjectContext:slideshow.managedObjectContext];
slideshow.image = image;
[image setValue:theImage forKey:#"image"];
CGSize size = theImage.size;
CGRect rect = CGRectMake(0.0, 0.0, .15 * size.width, .15 * size.height);
UIGraphicsBeginImageContext(rect.size);
[theImage drawInRect:rect];
slideshow.thumbnailImage1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self finishCameraImage];
return;
}
}
-(void)finishCameraImage {
if (editingImage1) {
keyInt = #"2";
[editedObject setValue:keyInt forKey:#"script1"];
pictureView.alpha = 0;
self.navigationItem.rightBarButtonItem = nil;
editedFieldKey = #"line2Int";
editedFieldName = NSLocalizedString(#"line2Int", #"display name for line2");
self.title = editedFieldName;
linePicker.hidden = NO;
}
I realize I am doing a lot to this image. If I put in a couple of delays would that help?