iPhone剪切技巧

本文介绍在iOS设备上实现图形剪切的技术细节。包括如何获取图形上下文、构造剪切路径、构建剪切区域以及粘贴图像等内容。通过具体的代码示例展示了如何使用Core Graphics API来完成这些操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iPhone中的剪切技巧: 

1.获取图形上下文 
2.构造剪切的路径(形状)

3.构建剪切区域 
4.贴上你的画

 

  1. // 1  
  2. CGContextRef context = UIGraphicsGetCurrentContext();  
  3.   
  4. // 2  
  5. CGRect bounds = CGRectMake(0.0f, 0.0f, SIDELENGTH, SIDELENGTH);  
  6. CGMutablePathRef path = CGPathCreateMutable();  
  7. CGPathAddEllipseInRect(path, NULL, bounds);  
  8.   
  9. // 3  
  10. CGContextAddPath(context, path);  
  11. CGContextClip(context);  
  12.   
  13. // 4  
  14. [LOGO drawInRect:bounds];  

-(UIImage*)getClipImageRect:(CGRect)cropRect

{

    CGImageRef imageRef =CGImageCreateWithImageInRect([self.photoCGImage], cropRect);

    UIImage *result = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);


    return result;

}


    // Create appropriately modified image.

UIImage *image = nil;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {

UIGraphicsBeginImageContextWithOptions(destRect.size, NO, 0.0); // 0.0 for scale means "correct scale for device's main screen".

CGImageRef sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect); // cropping happens here.

image = [UIImage imageWithCGImage:sourceImg scale:0.0 orientation:self.imageOrientation]; // create cropped UIImage.

[image drawInRect:destRect]; // the actual scaling happens here, and orientation is taken care of automatically.

CGImageRelease(sourceImg);

image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

}

#endif

if (!image) {

// Try older method.

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef context = CGBitmapContextCreate(NULL, fitSize.width, fitSize.height, 8, (fitSize.width * 4), 

colorSpace, kCGImageAlphaPremultipliedLast);

CGImageRef sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect);

CGContextDrawImage(context, destRect, sourceImg);

CGImageRelease(sourceImg);

CGImageRef finalImage = CGBitmapContextCreateImage(context);

CGContextRelease(context);

CGColorSpaceRelease(colorSpace);

image = [UIImage imageWithCGImage:finalImage];

CGImageRelease(finalImage);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值