How to retrive single image from png file in iphone - ios

I can access image's by using [UIImage imageNamed:#"nameOfPng.png"]; if I have a single image in a .png file. But if we are having a multiple images in a .png file, how do we access sub images from a single UIImage ?
This is the single .png file. If i wanted to get the red button image or any other button image how can I do this.

What you are looking to do is to create a clipped UIImage from a section of your current image. I usually do it this way, changing the clip rect each time. Where srcImage is your original image.
//Set the clip rectangle
CGRect clipRect = CGRectMake(0, 0, 100, 100);
//Get sub image
CGImageRef drawImage = CGImageCreateWithImageInRect(srcImage.CGImage, clipRect);
UIImage *newImage = [UIImage imageWithCGImage:drawImage];
CGImageRelease(drawImage);

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);

Crop image to a square according to the size of a UIView/CGRect

I have an implementation of AVCaptureSession and my goal is for the user to take a photo and only save the part of the image within the red square border, as shown below:
AVCaptureSession's previewLayer (the camera) spans from (0,0) (top left) to the bottom of my camera controls bar (the bar just above the view that contains the shutter). My navigation bar and controls bar are semi-transparent, so the camera can show through.
I'm using [captureSession setSessionPreset:AVCaptureSessionPresetPhoto]; to ensure that the original image being saved to the camera roll is like Apple's camera.
The user will be able to take the photo in portrait, landscape left and right, so the cropping method must take this into account.
So far, I've tried to crop the original image using this code:
DDLogVerbose(#"%#: Image crop rect: (%f, %f, %f, %f)", THIS_FILE, self.imageCropRect.origin.x, self.imageCropRect.origin.y, self.imageCropRect.size.width, self.imageCropRect.size.height);
// Create new image context (retina safe)
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.imageCropRect.size.width, self.imageCropRect.size.width), NO, 0.0);
// Create rect for image
CGRect rect = self.imageCropRect;
// Draw the image into the rect
[self.captureManager.stillImage drawInRect:rect];
// Saving the image, ending image context
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
However, when I look at the cropped image in the camera roll, it seems that it has just squashed the original image, and not discarded the top and bottom parts of the image like I'd like. It also results in 53 pixels of white space at the top of the "cropped" image, likely because of the y position of my CGRect.
This is my logging output for the CGRect:
Image crop rect: (0.000000, 53.000000, 320.000000, 322.000000)
This also describes the frame of the red bordered view in the superview.
Is there something crucial I'm overlooking?
P.S. The original image size (taken with a camera in portrait mode) is:
Original image size: (2448.000000, 3264.000000)
You can crop images with CGImageCreateWithImageInRect:
CGImageRef imageRef = CGImageCreateWithImageInRect([uncroppedImage CGImage], bounds);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
Don't forget to add scale parameter otherwise you will get low resolution image
CGImageRef imageRef = CGImageCreateWithImageInRect([uncroppedImage CGImage], CGRectMake(0, 0, 30, 120));
[imageView setImage:[UIImage imageWithCGImage:imageRef scale:[[UIScreen mainScreen] scale] orientation:UIImageOrientationUp]];
CGImageRelease(imageRef);
Swift 3:
let imageRef:CGImage = uncroppedImage.cgImage!.cropping(to: bounds)!
let croppedImage:UIImage = UIImage(cgImage: imageRef)

Image Cropping from Gallery

I want to crop selected image from a gallery (Programmatically). I have done a lot of research and got the [tutor](http://iosdevelopertips.com/graphics/how-to-crop-an-image.html) gone through this.still getting confuse whether cropping of image can be done by using UIImagePickerController or UIImageView.I'm not getting from where to start or how to start?. please suggest me the right way any one.
Answer : CGImage Reference
1) Create a rectangle that represents a cropped image from the middle of the existing image :
CGRect rect = CGRectMake(size.width / 4, size.height / 4 ,
(size.width / 2), (size.height / 2));
2) Create bitmap image from original image data, using rectangle to specify desired crop area :
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rect);
UIImage *img = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
3) Create and show the new image from bitmap data :
imageView = [[UIImageView alloc] initWithImage:img];
Useful Links :
1) Working with UIGestureRecognizers.
2) Cropping and Resizing Images from Camera in iOS and Objective-C.
GoodLuck !!!

How to save a UIImage with a mask

I have a UIImageView that can be moved/scaled (self.imageForEditing). On top of this image view I have an overlay with a hole cut out, which is static and can't be moved. I need to save just the part of the underlying image that is visible through the hole at the time a button is pressed. My current attempt:
- (IBAction)saveImage
{
UIImage *image = self.imageForEditing.image;
CGImageRef originalMask = [UIImage imageNamed:#"picOverlay"].CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(originalMask),
CGImageGetHeight(originalMask),
CGImageGetBitsPerComponent(originalMask),
CGImageGetBitsPerPixel(originalMask),
CGImageGetBytesPerRow(originalMask),
CGImageGetDataProvider(originalMask), nil, YES);
CGImageRef maskedImageRef = CGImageCreateWithMask(image.CGImage, mask);
UIImage *maskedImage = [UIImage imageWithCGImage:maskedImageRef scale:image.scale orientation:image.imageOrientation];
CGImageRelease(mask);
CGImageRelease(maskedImageRef);
UIImageView *test = [[UIImageView alloc] initWithImage:maskedImage];
[self.view addSubview:test];
}
As a test I'm just trying to add the newly created image to the top left of the screen. Theoretically it should be a small round image (the part that was visible through the overlay). But I'm just getting the whole image created again. What am I doing wrong? And how can I account for the fact that self.imageForEditing can be moved around?
CGImageCreateWithMask returns an image of the same size as the original's one.
That is why you get the original image (I assume) with the mask being applied.
You can apply the mask and then remove the invisible border. Use the advice from this question: iOS: How to trim an image to the useful parts (remove transparent border)
Find the bounds of the non-transparent part of the image and redraw it into a new image.

Crop UIImage according to Image Resolution

I have one UIImageView. Its content mode is set to AspectFit.
[imageView setContentMode:UIViewContentModeScaleAspectFit].
I need to crop a subImage from this image. This is the code which crops the image:
CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], customRect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
where customRect is the rectangle from which I need to crop the image.
This is how I calculate it:
CGRect customRect = CGRectMake((cropView.frame.origin.x/xFactor),
(cropView.frame.origin.y/yFactor),
(cropView.frame.size.width/xFactor),
(cropView.frame.size.height/yFactor));
The problem comes in cropping. CGImageCreateWithImageInRect crops the given area according to the actual image size which, in some cases, is larger than the image view size. I tried using other approaches such as UIGraphics:getImageFromCurrentImageContext but these do not keep the image quality as much as it degrades them.

Resources