剪切照片 crop Image (swift PK objec

2017-06-29  本文已影响1437人  coding_Liu

object-c 的方式:

方式1

    CGFloat scale = [UIScreen mainScreen].scale;
    CGRect rectFrame = CGRectMake(frameFromRect.origin.x * scale, frameFromRect.origin.y * scale, frameFromRect.size.width * scale, frameFromRect.size.height * scale);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], rectFrame);
    UIImage * croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    readingView.image = croppedImage;```

方式2

/**
*从图片中按指定的位置大小截取图片的一部分



swift 的方式: (直接剪切不惯病,直接剪切没毛病)

let screenScale = UIScreen.main.scale
let index = newView!.superview!.subviews.index(of: newView!)!
let subImageFrame = CGRect.init(x: frames[index].origin.xscreenScale, y: frames[index].origin.yscreenScale, width: frames[index].size.widthscreenScale, height: frames[index].size.heightscreenScale)

///核心代码就一句
let subImage = UIImage.init(cgImage:self.backImage!.cgImage!.cropping(to: subImageFrame)!)

newView?.image = subImage```

上一篇 下一篇

猜你喜欢

热点阅读