Ios taking snapshot of a screen after cropping some parts - ios

Yep as the title says I need to take a cropped snapshot of my app.
I want to cut top of the screenshot little bit (%20) I already have a code which I used to take a snapshot and send it to facebook and its working but its taking the photo of all of the screen so how can tell my code to ignore the %20 percent of the screen.Maybe with height and width also I looked some questions in the stack overflow and manage to slide my screenshot so I get rid of the unwanted part at the top but this time at the bottom huge white area appeared so it didnt solve my problem.
Here is my snapshot code
UIGraphicsBeginImageContext(self.ekran.bounds.size);
[self.ekran.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

A method to crop the image, that accepts any frame to crop the image against
- (UIImage *)cropImage:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
Use it as follows:
UIGraphicsBeginImageContext(self.ekran.bounds.size);
[self.ekran.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGFloat imgHight = resultingImage.size.height;
// Create a frame that crops the top 20% of the image
CGRect* imageFrame = CGRectMake(0, imgHight - (imgHight*0.8), width, imgHight*0.8);
resultingImage = [self cropImage:resultingImage toRect:imageFrame];

Related

How can I use "css sprites" in ios?

I have an image with multiple icons, and I have the position and the size of the icon that I want to show.
The question is, how can I show just part of an image in a UIImageView so I can show only the icon that I want to?
Is it possible to show the icon correctly in 1x, 2x, and 3x, even if the image gets a bit pixelated?
You can crop a part of the image and create a new UIImage from it with CGImageCreateWithImageInRect:
CGRect cropRect = CGRectMake(x,y,width,height); //Calculate the rect you'd like to show
CGImageRef imageRef = CGImageCreateWithImageInRect(originalImage.CGImage, cropRect);
UIImage* outImage = [UIImage imageWithCGImage:imageRef scale:originalImage.scale orientation:originalImage.imageOrientation];
CGImageRelease(imageRef);

UIImage doesn't want to fit in my Slide Show

In order to make a Slide Show, I installed the KASlideShow pod, which is perfect. On the iphone 5S, my Slideshow took all the width (What I wanted), but, when it comes to test the app on the iPhone 6, it takes only 3/4 of the screen.
I tried to scale the UIImage using this method :
CGRect rect = CGRectMake(0,0, width, height);
UIGraphicsBeginImageContext( rect.size );
[originialImage drawInRect:rect];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(finalImage);
UIImage *img=[UIImage imageWithData:imageData];
But it only scales to 3/4, even if I put enormous values. It's as if there was a limit.
I tried to use UIViewContentMode.ScaleAspectFill attribute, but it doesn't do anything.
I do not have more ideas to test :/
Thanks for your help.

how to crop image to image with offset and size? [duplicate]

This question already has answers here:
Cropping an UIImage
(25 answers)
Closed 8 years ago.
I'm making an application that need to crop the image to another image.
i want to crop the source Image ( like green rectangle) to destination image ( like white rectangle). I can get the size of source and destination image and the offset x and y . How can i got that crop image and save it to library?
You can see the image attach here:
How can I crop to that image? and if you can please give me an example source code
so much thanks for that
Use this method and pass image, rect as parameter. You can specify y offset and x offset in cropRect
-(UIImage *)cropImage:(UIImage *)image rect:(CGRect)cropRect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *cropedImg = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropedImg;
}
Check Below Code:
-(UIImage *)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Check below Links For reference:
http://code4app.net/ios/Image-crop-demo/501e1f3f6803faea5d000000
http://code4app.net/ios/Photo-Cropper-View-Controller/4f95519c06f6e7d870000000
http://code4app.net/ios/Image-Cropper/4f8cc87f06f6e7d86c000000
http://code4app.net/ios/Simple-Image-Editor-View/4ff2af4c6803fa381b000000
Get sample code from here. Then you can customise your own.

Crop Image using CGRect

I have been trying to do this since forever. I have a camera overlay. I want to get my final image to be the part of the image viewable from the in-built camera.
What I did was make CGRect with dimensions equal to the square in the camera. Then I tried cropping it using this function.
- (UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return croppedImage;
}
I called it like this
CGRect rect = CGRectMake(10, 72, 300, 300);
UIImage *realImage = [self imageByCropping:[self.capturedImages objectAtIndex:0] toRect:rect];
What I get is a bad quality image with the wrong orientation.
::EDIT::
With Nitin's answer I can crop the correct part of the screen but the problem is it crops the view that follows the camera view, 'the confirmation view'. I suspect this is because Nitin's code uses
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
and because the ViewController in which all this is happening because the View Controller for the Confirmation View is the Controller in which this code is being executed. I will try to explain this with a small map
CameraOverlay.xib(it uses this xib to create an overlay) <===== CameraOverlayViewController ---------> ConfirmationView
So when first the ViewController is evoked(button on Tab bar), it opens the camera(UIImagePickerController) with an overlay over it. Then once user clicks an image, the image is shown on the ConfirmationView.
What I think is happening is when
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, 1.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
these lines are being executed, the View at that time is ConfirmationView.
Note: I call the function in
(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method.
Refer Drawing and printing Guide.
The default coordinate system is different between CoreGraphics and UIKit. I think your issue is because of this fact.
Using these may help you solve the issue
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context , 0.0, rect.size.height);
CGContextScaleCTM(context , 1.0, -1.0);

Combine two images

I would like to take an image and duplicate it. Then increase it by 105% and overlay it on the original image.
What is the correct way to do this on iOS?
This is your basic code for drawing the image and then saving it as an image again:
- (UIImage *)renderImage:(UIImage *)image atSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0.0, 0.0, size.width, size.height)];
// draw anything else into the context
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
Where it says "draw anything else into the context" you can draw the image at a reduced size by setting the appropriate rect to draw in. Then, call the renderImage method with whatever size you want the full image to be. You can use CGContextSetAlpha to set the transparency.

Resources