gpt4 book ai didi

ios - 将 UIImage 转换为 NSData 并返回 UIImage

转载 作者:技术小花猫 更新时间:2023-10-29 11:10:36 28 4
gpt4 key购买 nike

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

因此您的示例可能如下所示:

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

关于ios - 将 UIImage 转换为 NSData 并返回 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013144/

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