- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
假设您从一个 UIImage 开始,您希望裁剪它。最常见的方法是使用 CGImageCreateWithImageInRect 获取 CGImageRef 并从中创建一个新图像,如下所示:
CGRect cropRect = CGRectMake(x*width, y*height, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
现在假设您稍后需要将此 UIImage 转换为 NSData 对象。例如,如果您希望使用 NSKeyedArchiver 将图像存储在磁盘上,就会发生这种情况。或者您可以通过显式执行以下操作来获取 NSData 对象:
NSData *imageData = UIImagePNGRepresentation(croppedImage);
我这样做了,后来我尝试通过以下方式重建一个新的 UIImage 对象:
UIImage *image = [UIImage imageWithData:imageData];
我为一系列 48 张图片做了这个。他们中的大多数都很好,但在每 4 张图像之后,接下来的 2 张图像被损坏,每张图像的顶部和底部交换。如果我取出转换为 NSData 并再次返回,它工作正常。如果我使用 NSKeyedArchiver 将图像数组写入磁盘(我猜它在其编码例程中调用类似 UIImagePNGRepresentation 的东西),我也会遇到完全相同的问题。我的理论是这一定与我如何从 CGImageRefs 构建图像有关。
解决这个问题的方法是什么?我似乎无法弄清楚如何将 CGImageRefs 放入 NSData 对象中,我更喜欢一种能够正确构建 UIImage 的解决方案,以便它可以被序列化。
编辑:
有些人要求实际运行的代码,所以我在这里添加它
UIImage *image = self.animationObject.image;
for (int y=0; y<[self.animationInfoObject.numInY intValue]; y++) {
for (int x=0; x<[self.animationInfoObject.numInX intValue]; x++) {
CGRect cropRect = CGRectMake(x*width, y*height, width, height);
CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);
UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
NSData *imageData = UIImageJPEGRepresentation(croppedImage, 1.0);
[self addImageToArray:imageData];
CFRelease(imageRef);
}
}
self.animationObject.image 中的图像是一个图像网格,每个方向都有 numInX 和 numInY 个图像。我从 2D 网格中裁剪出每个单独的图像并保存它们。
稍后,我将图像放在 ImageView 中以使其具有动画效果。这是代码:
NSMutableArray *animationArray = [[NSMutableArray alloc] init];
for (int frame=0; frame<[[photoObject frameArray] count]; frame++) {
NSData *imageData =[[photoObject imageArray] objectAtIndex:[[[photoObject frameArray] objectAtIndex:frame] integerValue]-1];
UIImage *image = [UIImage imageWithData:imageData];
[animationArray addObject:image];
}
这在 iPhone 6 上一切正常,但在 iPhone 5 上不起作用。这似乎也只有在图像网格很长时才会出现问题。例如,在我使用 self.animationObject.image 的示例中,numInY 为 3,numInX 为 2,图像的总大小为 1536x3072(每个迷你图像为 768x1024)。它是底部的两个图像,它们的顶部和底部翻转了。
最佳答案
您的问题是由于图像方向引起的。
每个 UIImage 都包含一个 -imageOrientation
属性,当您请求 CGImage 时,该属性定义了渲染到屏幕后应应用于
你丢失了那个信息。UIImage
的方向
如果您从所提供的元数据字典中的相机获取图像,则还有 EXIF 方向,因此它取决于您的数据来自何处。
在我的博客上,我写了一个 article关于这一点以及如果图像来自相机,如何正确处理它们。
一个快速的解决方案是读取 CGImage 属性并根据 -imageOrientation
属性应用转换。你可以找到不同的方法,here和 here
此片段摘自 here , 归功于 awolf
- (UIImage *)croppedImageInRect:(CGRect)rect
{
double (^rad)(double) = ^(double deg) {
return deg / 180.0 * M_PI;
};
CGAffineTransform rectTransform;
switch (self.imageOrientation) {
case UIImageOrientationLeft:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(90)), 0, -self.size.height);
break;
case UIImageOrientationRight:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-90)), -self.size.width, 0);
break;
case UIImageOrientationDown:
rectTransform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rad(-180)), -self.size.width, -self.size.height);
break;
default:
rectTransform = CGAffineTransformIdentity;
};
rectTransform = CGAffineTransformScale(rectTransform, self.scale, self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], CGRectApplyAffineTransform(rect, rectTransform));
UIImage *result = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return result;
}
关于ios - 如何将从 CGImageRef 创建的 UIImage 对象转换为原始数据并再次转换回来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29441298/
在更大的应用程序的上下文中,我的小程序需要将一些数据打印到 Zebra 或 Dymo(取决于用户安装的内容)标签打印机。 我收到的数据是转义形式,我只需要发送到打印机并让它解释它的数据。 搜索我找到了
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭去年。 Improve th
我正在尝试使用 GetUserMedia() 从用户麦克风录制和保存声音片段和 AudioContext蜜蜂。 我已经能够使用 MediaRecorder 做到这一点API,但不幸的是,Safari/
我想编写一个将十六进制数据转换为音频文件的小型Web应用程序。 音频文件的数据将采用十六进制格式,如下所示: DA1FFFF8B3AEEE2E23BBB9A2221F10400180001EF1C1E
在其中一个 API 中,我收到以下 Json 响应:您可以在此处查看此响应示例 Sample Json resopnse { "histogram" : { "1" : "12
如何在 python 上使用 Resuests 库发布原始数据?我正在尝试登录。 Json 抛出异常 TypeError: set(['"clienteLogin":{"Token":"b94261f
有人告诉我,无论何时使用字节,都应该将变量声明为无符号字符。在 Windows 的数据类型中,BYTE 被声明为 unsigned char。 我的问题: 为什么? Unsigned 是从 0 到 2
如何读取 GPS 原始数据,更具体地说,我需要卫星伪距。此数据不提供 NMEA 格式。 最佳答案 卫星伪距在 official API 中不可用 ,既不通过 GpsStatus.Listener 也不
给定以下 XML: 1424 我正在尝试获取
我使用了以下代码将十进制的 bigint 转换为 bytearray(原始数据),但我得到了错误的结果。 这里有什么错误吗? 我正在 Apple Mac 中尝试此操作(适用于 Iphone 应用程序)
我在 iOS 应用程序中使用 Firebase 登录时遇到了表格 View 问题。该表从子提要加载内容。当我第一次登录时,表加载正常,但如果我注销并再次登录,表会重新加载所有数据,将原始数据添加到表的
我正在使用 Apache BCEL动态创建 java 类,每个类都有自己的关联图像。这些生成的类实现了以下接口(interface): interface ImageOwner { byte[
有没有办法读取 Sim 卡的“原始”数据?类似于如何使用 Pdu 的 SmsMessage 原始数据读取原始数据? 最佳答案 阅读SIM卡相关信息需要TelephonyManager API . Te
有没有办法在命令行 curl 中将数据 POST 或 GET(插入您最喜欢的 HTTP 方法)数据到 URL 并包含在原始发布的数据 header 值中,而不是发出 -H 选项? 例如: $curl
我正在开展一个项目,尝试使用 Myo Gesture Control Armband 识别一些用于康复治疗的 Action /姿势。 . 我有三个问题: EMG Raw Data 返回的最大值和最小值
我有 flv 文件,其中包含带有 aac 原始数据的音频标签。每个音频标签都有一组 aac 原始数据。原始数据有不同的大小。我想通过 RTP 发送。我添加了 13 位大小的 AU header 。它是
我使用制造商提供的库通过 USB 访问相机。我通过结构接收有关图像的信息: typedef struct { /*! Buffer handle which contains new data
我正在从事 BLE 项目,其中录音机硬件连续流式传输数据并发送到 iOS 应用程序。从 iOS 应用程序端,我需要读取传输的数据。 硬件向 iOS 应用程序发送 HEX 数据,我们需要创建 .mp3/
我正在尝试使用 AFNetworking 发出 HTTP PUT 请求以在 CouchDB 服务器中创建附件。服务器需要 HTTP 正文中的 base64 编码字符串。如何在不使用 AFNetwork
我有一些 Graylog2 使用 syslog 输入插件接收的日志。 我需要以我收到的相同格式导出它们,无需任何解析,以便我可以发送给应用程序支持团队。 我已将 INPUT 设置为保留 full_ma
我是一名优秀的程序员,十分优秀!