A detailed approach to intercepting and scaling uiimage in IOS application development _ios

Source: Internet
Author: User
Tags scale image uikit

Intercepting uiimage specified size area
recently encountered the need: from the server to get a photo, only need to display his left half, or the middle part, and so on. That is, intercepting uiimage the specified size area.

UIImage Extensions:

My solution is to extend the uiimage. The interception is done through Cgimageref and cgimage, and the method invoked is: Cgimagecreatewithimageinrect. The extension class is called Uiimage+crop, and the specific code is as follows:

Uiimage+crop.h

#import <UIKit/UIKit.h> typedef ns_enum (Nsinteger, xycropimagestyle) {xycropimagestyleright = 0,//right Half Xycropimagestylecenter = 1,//middle part Xycropimagestyleleft = 2,//left half Xycropimagestylerightoneofthir D = 3,//Right One-third part Xycropimagestylecenteroneofthird = 4,//middle One-third Part Xycropimagestyleleftoneofthird = 5,//left One-third part Xycropimagestylerightquarter = 6,//Right One-fourth part Xycropimagestylecenterrightquarter = 7,//Middle right One-fourth part XYCR

Opimagestylecenterleftquarter = 8,//center left One-fourth Part xycropimagestyleleftquarter = 9,//left One-fourth part};

@interface uiimage (Crop)-(UIImage *) Imagebycroppingwithstyle: (xycropimagestyle) style; @end uiimage+crop.m #import "Uiimage+crop.h" @implementation uiimage (Crop)-(UIImage *) Imagebycroppingwithstyle: (XYCR
  Opimagestyle) style {CGRect rect;
      switch (style) {Case xycropimagestyleleft:rect = CGRectMake (0, 0, SELF.SIZE.WIDTH/2, self.size.height);
    Break Case XycropimagestylecEnter:rect = CGRectMake (SELF.SIZE.WIDTH/4, 0, SELF.SIZE.WIDTH/2, self.size.height);
    Break
      Case xycropimagestyleright:rect = CGRectMake (SELF.SIZE.WIDTH/2, 0, SELF.SIZE.WIDTH/2, self.size.height);
    Break
      Case xycropimagestyleleftoneofthird:rect = CGRectMake (0, 0, SELF.SIZE.WIDTH/3, self.size.height);
    Break 
      Case xycropimagestylecenteroneofthird:rect = CGRectMake (SELF.SIZE.WIDTH/3, 0, SELF.SIZE.WIDTH/3, self.size.height);
    Break Case xycropimagestylerightoneofthird:rect = CGRectMake (self.size.width/3*2, 0, SELF.SIZE.WIDTH/3, self.size.height)
      ;
    Break
      Case xycropimagestyleleftquarter:rect = CGRectMake (0, 0, SELF.SIZE.WIDTH/4, self.size.height);
    Break Case xycropimagestylecenterleftquarter:rect = CGRectMake (SELF.SIZE.WIDTH/4, 0, SELF.SIZE.WIDTH/4, self.size.height)
      ;
    Break Case xycropimagestylecenterrightquarter:rect = CGRectMake (self.size.width/4*2, 0, SELF.SIZE.WIDTH/4, Self.size.height);
    Break
      Case xycropimagestylerightquarter:rect = CGRectMake (self.size.width/4*3, 0, SELF.SIZE.WIDTH/4, self.size.height);
    Break
  Default:break; } cgimageref imageref = self.
  Cgimage;
  Cgimageref imagepartref = Cgimagecreatewithimageinrect (imageref, rect);
  UIImage *cropimage = [UIImage imagewithcgimage:imagepartref];
  Cgimagerelease (IMAGEPARTREF);
return cropimage;

 }

Practical application:

Simply test to see if there is any effect we want to achieve. First, load a complete uiimageview first. This should not be difficult. The code is as follows:

Uiimageview *imgview = [[Uiimageview alloc] init];
Imgview.frame = CGRectMake (screen.width-226)/2;
UIImage *image = [uiimage imagenamed:@ "Ganggang"];
Imgview.image = image;
[Self.view Addsubview:imgview];

Run it:

To crop the uiimage, first import the header file:

#import "Uiimage+crop.h"

In the above uiimage *image = [uiimage imagenamed:@ "Ganggang"]; This code adds the following sentence:

image = [Image imagebycroppingwithstyle:xycropimagestyleleft];

The Xycropimagestyleleft is the left half of the captured photograph. The effect is as follows:

Interception success, but also can intercept other areas, only need to pass into different xycropimagestyle can be achieved.

UIImage Scaling
The Intercept uiimage specifies the size area, and it is convenient to intercept the uiimage. To share with you today is the uiimage scaling.

Two types of scaling:

    • Zooms to the specified size, which is the specified amount.
    • Equal scaling.

1. Zoom to specified size

-(uiimage*) Imagecompresswithsimple: (uiimage*) Image scaledtosize: (cgsize) size
{
  Uigraphicsbeginimagecontext (size);
  [Image Drawinrect:cgrectmake (0,0,size.width,size.height)];
  uiimage* newimage = Uigraphicsgetimagefromcurrentimagecontext ();
  Uigraphicsendimagecontext ();
  return newimage;
}

2. Equal-ratio Scaling

(1) by scaling factor:

-(uiimage*) Imagecompresswithsimple: (uiimage*) Image scale: (float) scale
{
  cgsize size = image.size;
  CGFloat width = size.width;
  CGFloat height = size.height;
  CGFloat scaledwidth = width * scale;
  CGFloat scaledheight = height * scale;
  Uigraphicsbeginimagecontext (size); This is crop
  [image Drawinrect:cgrectmake (0,0,scaledwidth,scaledheight)];
  uiimage* newimage= Uigraphicsgetimagefromcurrentimagecontext ();
  Uigraphicsendimagecontext ();
  return newimage;
}

Scale is the scaling factor.

(2) The scaling factor is obtained by calculation

-(uiimage*) Imagebyscalingandcroppingforsize: (cgsize) targetsize {uiimage *sourceimage = [UIImage imageNamed:@] Test.j
  PG "];
  UIImage *newimage = nil;
  Cgsize imagesize = sourceimage.size;
  CGFloat width = imagesize.width;
  CGFloat height = imagesize.height;
  CGFloat targetwidth = targetsize.width;
  CGFloat targetheight = targetsize.height;
  CGFloat scalefactor = 0.0;
  CGFloat scaledwidth = targetwidth;
  CGFloat scaledheight = targetheight;

  Cgpoint thumbnailpoint = Cgpointmake (0.0,0.0);
    if (Cgsizeequaltosize (imagesize, targetsize) = NO) {cgfloat widthfactor = targetwidth/width;
    CGFloat heightfactor = targetheight/height; if (Widthfactor > Heightfactor) scalefactor = Widthfactor; Scale to fit height else scalefactor = heightfactor;
    Scale to fit width scaledwidth= width * scalefactor;
    Scaledheight = height * scalefactor; Center the image if (Widthfactor > Heightfactor) {thumbnailpoint.y = (Targetheight-scaledheight) * 0.5;
    else if (Widthfactor < heightfactor) {thumbnailpoint.x = (targetwidth-scaledwidth) * 0.5; } uigraphicsbeginimagecontext (Targetsize);
  This is crop cgrect thumbnailrect = Cgrectzero;
  Thumbnailrect.origin = Thumbnailpoint;
  Thumbnailrect.size.width= Scaledwidth;
  ThumbnailRect.size.height = Scaledheight;
  [Sourceimage Drawinrect:thumbnailrect];

  NewImage = Uigraphicsgetimagefromcurrentimagecontext ();
  if (newimage = = nil) NSLog (@ "Could not scale image");

  Pop the "context" to "to" the default Uigraphicsendimagecontext ();

return newimage;
 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.