Cropping UIimage in IOS -


I am very new to iOS and my first work is cropping the image. It means that if I were to create a banner image I am using an image and the given frame size is either the size of the small thn image. My code should automatically resize the image in the relative aspect ratio of the image and then set the picture in that frame

< P> I have done R & D and after that I have written the code

- (UIImage *) MyScaleNEwMethodwithImage: (UIImage *) image and frame (Sijiartiatti) frame {float Bimaciiiiii = Cvisaijh. height; Float bmWidth = image.size.width; UIImage * Received Image = Image; If (bmhite> bmwdath) {float ratio = frame. Size.height / frame.size.width; Float newbmHeight = ratio * BMWWithth; Float Cutted = (BMHite-NBMHite) / 2; If (Cropped Height & Lt; 0) {Float Ratio 1 = frame.edame.width / frame.size.height; Float newbmHeight1 = (ratio 1 * bmHite); Float cropimage height 1 = (BMW-NE-NBM HEIGHT 1) / 2; CGRTT clactct = CGRactack (CrossSpnimite Heat1, 0, BMWWith-Cropidize Heat 1, BMHEIT); CGImageRef imref = CGImageCreateWithImageInRect ([Image CGImage], Clint); UIImage * newSubImage = [UIImage imageWithCGImage: imref]; Return new evidence; } And {CGRTEELECTIC = CGRactMake (0, CROPED HEIGHT, BMW WTH, BMHITE-CROCKED HEAIT); CGImageRef imref = CGImageCreateWithImageInRect ([Image CGImage], Clint); UIImage * newSubImage = [UIImage imageWithCGImage: imref]; Return new evidence; }} And {float ratio = frame. Size.height / frame.size.width; Float newbmHeight = proportion * BMHEIT; Float chopped = = (BMHEIT-NBMHYT) / 4; If (Cropped Height & Lt; 0) {Float Ratio 1 = frame.edame.width / frame.size.height; Float newbmHeight1 = (ratio 1 * bmwdath); Float cropimage height 1 = (BMW-NE-NBM HEIGHT 1) / 2; UIImageView * DummyImage = [[UIImageView alloc] initWithFrame: CGRactMake (0, CrossSmageHeight1, BMWWithth, (BMHite-Crospenomite Hight 1)); [Dummy image set image: receiver image]; CGImageRef imageRef = CGImageCreateWithImageInRect ([DummyImage.image CGImage], CGRectMake (0, cropedimageHeight1 / 2, BMW / 2, (BMHite-CroppyImite Heat 1) / 2)); // or use UIImage wherever you want [DummyImage setImage: [UIImage imageWithCGImage: imageRef]]; CGImageRelease (imageRef); UIImage * Scale Image = [UIImage imageWithCGImage: imageRef]; Return scale image; } Else {UIImageView * DummyImage = [[UIImageView alloc] initWithFrame: CGRectMake (Cut Off High, 0, BMWD-RoP, HEIGHT, BMHEIT)]; [Dummy image set image: receiver image]; CGImageRef imageRef = CGImageCreateWithImageInRect ([DummyImage.image CGImage], CGRactMake (Crop Head Hi, 2 * Cut Off High, (BMWD-Croped Height), BMHit / 2); // or use UIImage wherever you want [DummyImage setImage: [UIImage imageWithCGImage: imageRef]]; CGImageRelease (imageRef); UIImage * Scale Image = [UIImage imageWithCGImage: imageRef]; Return scale image; }}}

In my frame I am getting the necessary picture, but when the screen changes, then I can see the whole image I want to cut the unwanted image

< / Div>

This piece of code can help you

  - (CGRT) crop image: (CGRCTAT) frame {nsassart (set contentmode == UIViewContentModeScaleAspectFit, @ "Content Mode aspect must fit"); CGFloat wScale = self.bounds.size.width / self.image.size.width; CGFloat hScale = self.bounds.size.height / self.image.size.height; Float x, y, w, h, offset; If (wScale & lt; hScale) {offset = (self.bounds.size.height - (self.image.size.height * widthScale)) / 2; X = frame.corein x / wScale; Y = (frame.origin.y- offset) / wScale; W = frame. Size.width / wScale; H = frame. Size.height / wScale; } And {offset = (self.bounds.size.width - (self.image.size.width * heightScale)) / 2; X = (frame.origin.x- offset) / hScale; Y = frame.corein.a/hscill; W = frame.size.width / hScale; H = frame. Size.height / hScale; } Return CGRectMake (X, Y, W, H); }  

Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -