我的服务器发送图片(base64)以及每个请求的时间戳,我只是将SDWebImage集成到我的应用程序中,我想知道如何访问SDWebImage从中获取的原始NSData
[imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@""]]];
这样我可以将文字与图像分开
答案 0 :(得分:0)
您可以使用此方法
[UIImage sd_imageWithData:[NSData dataWithContentOfURL:[NSURL URLWithString:urlStr]];
答案 1 :(得分:0)
您想将存储在NSData对象中的字节转换为UIImage对象,对吗?
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;
}
如果要跳过使用第三方实用程序在NSData对象中存储图像字节:
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;
}