如何放大和裁剪 UIImage?

发布于 2024-09-17 12:26:28 字数 745 浏览 6 评论 0原文

这是我的代码,但它崩溃了......有什么想法吗?

UIImage *tempImage = [[UIImage alloc] initWithData:imageData];
CGImageRef imgRef = [tempImage CGImage];
 [tempImage release];

 CGFloat width = CGImageGetWidth(imgRef);
 CGFloat height = CGImageGetHeight(imgRef);
 CGRect bounds = CGRectMake(0, 0, width, height);
 CGSize size = bounds.size;

 CGAffineTransform transform = CGAffineTransformMakeScale(4.0, 4.0);

 UIGraphicsBeginImageContext(size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextConcatCTM(context, transform);
 CGContextDrawImage(context, bounds, imgRef);
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

我在这里缺少什么?基本上只是尝试放大图像并将其裁剪为与原始大小相同。

谢谢

Here's the code I have but it's crashing ... any ideas?

UIImage *tempImage = [[UIImage alloc] initWithData:imageData];
CGImageRef imgRef = [tempImage CGImage];
 [tempImage release];

 CGFloat width = CGImageGetWidth(imgRef);
 CGFloat height = CGImageGetHeight(imgRef);
 CGRect bounds = CGRectMake(0, 0, width, height);
 CGSize size = bounds.size;

 CGAffineTransform transform = CGAffineTransformMakeScale(4.0, 4.0);

 UIGraphicsBeginImageContext(size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextConcatCTM(context, transform);
 CGContextDrawImage(context, bounds, imgRef);
 UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

What am I missing here? Basically just trying to scale image up and crop it to be same size as original.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2024-09-24 12:26:28

问题是这一行:

CGImageRef imgRef = [tempImage CGImage];

或者更准确地说,是这一行的直接后续内容:

[tempImage release];

您在这里获取一个 CF 对象,CGImageRef。 Core Foundation 对象只有保留/释放内存管理,但没有自动释放对象。因此,当您释放第二行中的 UIImage 时,CGImageRef 也会被删除。这再次意味着当您尝试将其绘制到那里时,它是未定义的。

我可以想到三个修复方法:

  • 使用 autorelease 来延迟图像的释放: [tempImage autorelease];
  • 将释放移动到方法的最底部
  • 保留并使用 CFRetain< 释放图像/code> 和 CFRelease

The problem is this line:

CGImageRef imgRef = [tempImage CGImage];

Or more precise, the direct follow-up of this line:

[tempImage release];

You are getting a CF object here, the CGImageRef. Core Foundation object only have the retain/release memory management, but no autoreleased objects. Hence, when you release the UIImage in the second row, the CGImageRef will be deleted as well. And this again means that it's undefined when you try to draw it down there.

I can think of three fixes:

  • use autorelease to delay the release of the image: [tempImage autorelease];
  • move the release to the very bottom of your method
  • retain and release the image using CFRetain and CFRelease.
夢归不見 2024-09-24 12:26:28

试试这个:

-(CGImageRef)imageCapture
{
    UIGraphicsBeginImageContext(self.view.bounds.size);
   [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   CGRect rect= CGRectMake(0,0 ,320, 480);

   CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
   return imageRef;
}

只要你想捕获屏幕,就使用下面的行

UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];

Try this one:

-(CGImageRef)imageCapture
{
    UIGraphicsBeginImageContext(self.view.bounds.size);
   [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   CGRect rect= CGRectMake(0,0 ,320, 480);

   CGImageRef imageRef = CGImageCreateWithImageInRect([viewImage CGImage], rect);
   return imageRef;
}

use the below line whenever you want to capture the screen

UIImage *captureImg=[[UIImage alloc] initWithCGImage:[self imageCapture]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文