简体   繁体   English

ios中uiimageView的字节数组

[英]byte array to uiimageView in ios

My requirement is that a local image will be converted to a byte array and then the byte array is converted back to the image 我的要求是将本地图像转换为字节数组,然后将字节数组转换回图像

i have done the image to Byte array conversion but i trying bytearray convert to image and then display on UIIMageView code is no error but Image Converted to byte array but byte array converted io image but image is not display please give me any idea 我已经完成了图像到字节数组的转换,但是我尝试将字节数组转换为图像,然后在UIIMageView代码上显示没有错误,但是图像已转换为字节数组,但是字节数组转换了io图像,但图像未显示,请给我任何想法

Here is what I have so far 这是我到目前为止的

   UIImage *image = [UIImage imageNamed: @"shopper-home-copy_03.png"];




   //Output
   NSData *data = UIImagePNGRepresentation(image);

    NSUInteger len = data.length;

    uint8_t *bytes = (uint8_t *)[data bytes];

    NSMutableString *result = [NSMutableString stringWithCapacity:len * 3];


    [result appendString:@"["];

    for (NSUInteger i = 0; i < len; i++)
    {
        if (i)
        {

            [result appendString:@","];

        }

        [result appendFormat:@"%d", bytes[i]];
    }

    [result appendString:@"]"];
   NSMutableData *newdata = [NSMutableData new];
    NSScanner *theScanner = [NSScanner scannerWithString: result];
    while ([theScanner isAtEnd] == NO) {
        int a;
        [theScanner scanInt:&a];
        uint8_t b = a;
        [newdata appendBytes:(const void *)&b length:1];
    }

    UIImage *newImage = [UIImage imageWithData:newdata];
    self->imageView = [[UIImageView alloc] initWithImage:newImage];

    imageView.frame = CGRectMake(0, 25 , 150,150);

    [self.view addSubview:self->imageView];

First of all you should check is Base64 is suitable for you. 首先,您应该检查Base64是否适合您。 If you still need your own format try following 如果您仍然需要自己的格式,请尝试以下操作

NSMutableData *newData = [NSMutableData new];
NSScanner *theScanner = [NSScanner scannerWithString: result];
theScanner.charactersToBeSkipped = [NSCharacterSet characterSetWithCharactersInString:@"[],"];
while ([theScanner isAtEnd] == NO) {
    int a;
    [theScanner scanInt:&a];
    uint8_t b = a;
    [newData appendBytes:(const void *)&b length:1];
}
UIImage *newImage = [UIImage imageWithData:newData];

Create an NSData object from the byte array. 从字节数组创建一个NSData对象。 And then use this 然后用这个

UIImage *image = [UIImage imageWithData:data];

Try this to convert string to bytes array.. 尝试将字符串转换为字节数组。

first step to convert mutable string to array. 将可变字符串转换为数组的第一步。

NSArray *strings = [<mutableString> componentsSeparatedByString:@","];

unsigned c = strings.count; // strings is the array instance from the mutable string (separate using ",")
uint8_t *bytes = malloc(sizeof(*bytes) * c);

unsigned i;

for (i = 0; i < c; i++)

{
NSString *str = [strings objectAtIndex:i];

int byte = [str intValue];
bytes[i] = byte;
}

from bytes convert to image is as follows.. 从字节转换为图像如下。

NSData *imageData = [NSData dataWithBytesNoCopy:bytes length:c freeWhenDone:YES];
UIImage *image = [UIImage imageWithData:imageData];

reference from Byte Array to NSData 字节数组到NSData的引用

Convert byteArray to NSData and then to UIImage byteArray转换为NSData ,然后转换为UIImage

-(UIImage *)convertToImageFromByteArray:(NSMutableString*)mutableStr{

    NSArray *byteArray = [mutableStr componentsSeparatedByString:@","];

    unsigned c = byteArray.count;
    uint8_t *bytes = malloc(sizeof(*bytes) * c);

    unsigned i;
    for (i = 0; i < c; i++)
    {
        NSString *str = [byteArray objectAtIndex:i];
        int byte = [str intValue];
        bytes[i] = (uint8_t)byte;
    }

    NSData *data = [NSData dataWithBytes:bytes length:c];

    return [UIImage imageWithData:data];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM