将UIImage转换为NSData并返回UIImage [英] convert UIImage to NSData and back to UIImage

查看:106
本文介绍了将UIImage转换为NSData并返回UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从数据库中填充表格单元格,并且图像在设备上。
我将该图像存储在 imageDataObject 中,如何将 UIImage 转换为 NSData的
请建议,我尝试了很多解决方案,但它没有将 NSData 转换回 UIImage

I have to populate table cell from database and images are on device. I've to store that image in imageDataObject, how to convert that UIImage to NSData. Please suggest, I tried many solution but it's not converting back that NSData to UIImage.

    cell.textLabel.text = [[offlineImageListArray objectAtIndex:indexPath.row] imageName];
    UIImage *thumbnail = [self retrieveImageFromDevice:[[offlineImageListArray objectAtIndex:indexPath.row] imageName]];
    NSData* data = ??????????;
    ImageData *imageDataObject = [[ImageData alloc] initWithImageId:[[offlineImageListArray objectAtIndex:indexPath.row]imageId] 
    imageName:[[offlineImageListArray objectAtIndex:indexPath.row] imageName] imageData:data];
    [imagesArray addObject:imageDataObject];


推荐答案

转换 UIImage NSData ,使用 UIImageJPEGRepresentation(UIImage * image,CGFloat compressionQuality) UIImagePNGRepresentation(UIImage * image)

NSData 转换为 UIImage ,使用 [UIImage imageWithData:imageData]

所以你的例子可能如下所示:

So your example could look like this:

cell.textLabel.text = [[offlineImageListArray objectAtIndex:indexPath.row] imageName];
UIImage *thumbnail = [self retrieveImageFromDevice:[[offlineImageListArray objectAtIndex:indexPath.row] imageName]];
NSData* data = UIImagePNGRepresentation(thumbnail);
ImageData *imageDataObject = [[ImageData alloc] initWithImageId:[[offlineImageListArray objectAtIndex:indexPath.row]imageId] imageName:[[offlineImageListArray objectAtIndex:indexPath.row] imageName] imageData:data];
[imagesArray addObject:imageDataObject];

参考文献: https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html
https://developer.apple.com/Library/ios/documentation/ UIKit / Reference / UIImage_Class / Reference / Reference.html

这篇关于将UIImage转换为NSData并返回UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆