gpt4 book ai didi

c# - Objective C 中的 UIImage 到十六进制转换

转载 作者:行者123 更新时间:2023-11-29 03:45:51 24 4
gpt4 key购买 nike

我有以下 C# 编码将上传的位图图像转换为十六进制。我需要遵循 Objective-C 中具有相同目标的算法来获取 ip。假设我有一个 UIImageView,其中包含从图片库中选择的图像。我需要将此选定的图像转换为十六进制并写入文本文件。

请有人帮助我将此代码转换为 Objective C。我对 IOS 开发有一些了解,但对图像处理了解不多。

这是我的代码,

            Bitmap bm1 = (Bitmap)pictureBox1.Image;

int PictureWidth = pictureBox1.Image.Width;
int PictureHeight = pictureBox1.Image.Height;
int PictureX;
int PictureY;

int NVImageWidth;
int NVImageHeight;

NVImageWidth = PictureWidth;
NVImageHeight = PictureHeight;

int Quotient;
int Remainder;
int wp;

wp = 0;

StreamWriter sw = new StreamWriter("D:\\Test.txt");

for (PictureX = 0; PictureX <= PictureWidth - 1; PictureX++)
{
for (PictureY = 0; PictureY <= NVImageHeight - 1; PictureY++)
{
Color c1 = bm1.GetPixel(PictureX, PictureY);

if ((PictureY % 8) == 0)
{
wp = 0;
wp = (c1.G!=0) ? 0 : 1;
}
else if ((PictureY % 8) == 7)
{
wp = (wp << 1) | ((c1.G!=0) ? 0 : 1);
sw.Write(string.Format("%{0:x2}", wp));
}
else
{
wp = (wp << 1) | ((c1.G!=0) ? 0 : 1);
}
}
}

sw.Close();

最佳答案

我有一个建议:

首先将UIImage转换为NSData。然后将 NSData 转换为十六进制。

例如,

//converting to hexadecimal
-(NSString*)hexRepresentationWithSpaces_AS:(BOOL)spaces
{
const unsigned char* bytes = (const unsigned char*)[self bytes];
NSUInteger nbBytes = [self length];
//If spaces is true, insert a space every this many input bytes (twice this many output characters).
static const NSUInteger spaceEveryThisManyBytes = 4UL;
//If spaces is true, insert a line-break instead of a space every this many spaces.
static const NSUInteger lineBreakEveryThisManySpaces = 4UL;
const NSUInteger lineBreakEveryThisManyBytes = spaceEveryThisManyBytes * lineBreakEveryThisManySpaces;
NSUInteger strLen = 2*nbBytes + (spaces ? nbBytes/spaceEveryThisManyBytes : 0);

NSMutableString* hex = [[NSMutableString alloc] initWithCapacity:strLen];
for(NSUInteger i=0; i<nbBytes; ) {
[hex appendFormat:@"%02X", bytes[i]];
//We need to increment here so that the every-n-bytes computations are right.
++i;

if (spaces) {
if (i % lineBreakEveryThisManyBytes == 0) [hex appendString:@"\n"];
else if (i % spaceEveryThisManyBytes == 0) [hex appendString:@" "];
}
}
return [hex autorelease];
}

NSData *imageData = UIImagePNGRepresentation(myImage.image);
NSString* hex = [imageData hexRepresentationWithSpaces_AS:YES];

关于c# - Objective C 中的 UIImage 到十六进制转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com