0

My server sends a picture(base64) along with a timestamp per request and i just integrated SDWebImage into my app, i'd like to know how to access the raw NSData that SDWebImage gets from

[imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@""]]];

that way i could separate the text from the image

  • if the data contains more than just an image, you should not be downloading it directly to an image view. Instead use an NSURLConnection or NSURLSession to get the data, then parse out the contents yourself – Patrick Goley Jul 31 '14 at 22:27
  • @PatrickGoley so is there a delegate method where i can implement then parse the content in before giving SDWebImage the UIImage from it? –  Jul 31 '14 at 22:42
  • you should use something other than SDWebImage to download the data, since it is not purely image data. – Patrick Goley Jul 31 '14 at 22:47

2 Answers2

0

You can use this method,

[UIImage sd_imageWithData:[NSData dataWithContentOfURL:[NSURL URLWithString:urlStr]];

0

You want to turn bytes stored in an NSData object into a UIImage object, right?

UIImage *ImageFromBytes(NSData *data, CGSize targetSize)
{
    // Check data
    int width = targetSize.width;
    int height = targetSize.height;
    if (data.length < (width * height * 4))
    {
        NSLog(@"Error: Got %d bytes. Expected %d bytes",
            data.length, width * height * 4);
        return nil;
    }

    // Create a color space
    CGColorSpaceRef colorSpace =
        CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        NSLog(@"Error creating RGB colorspace");
        return nil;
    }

    // Create the bitmap context
    Byte *bytes = (Byte *) data.bytes;
    CGContextRef context = CGBitmapContextCreate(
        bytes, width, height,
        BITS_PER_COMPONENT, // 8 bits per component
        width * ARGB_COUNT, // 4 bytes in ARGB
        colorSpace,
        (CGBitmapInfo) kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(colorSpace );
    if (context == NULL)
    {
        NSLog(@"Error. Could not create context");
        return nil;
    }

    // Convert to image
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *image = [UIImage imageWithCGImage:imageRef];

    // Clean up
    CGContextRelease(context);
    CFRelease(imageRef);

    return image;
}

If you want to skip using a third-party utility for storing image bytes in an NSData object:

NSData *BytesFromRGBImage(UIImage *sourceImage)
{
    if (!sourceImage) return nil;

    // Establish color space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    if (colorSpace == NULL)
    {
        NSLog(@"Error creating RGB color space");
        return nil;
    }

    // Establish context
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    CGContextRef context = CGBitmapContextCreate(
        NULL, width, height,
        8, // bits per byte
        width * 4, // bytes per row
        colorSpace,
        (CGBitmapInfo) kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL)
    {
        NSLog(@"Error creating context");
        return nil;
    }

    // Draw source into context bytes
    CGRect rect = (CGRect){.size = sourceImage.size};
    CGContextDrawImage(context, rect, sourceImage.CGImage);

    // Create NSData from bytes
    NSData *data =
        [NSData dataWithBytes:CGBitmapContextGetData(context)
            length:(width * height * 4)]; // bytes per image
    CGContextRelease(context);
    return data;
}
James Bush
  • 1,485
  • 14
  • 19