删除存储为核心数据实体属性的图像

Delete Image Stored As Attribute of Core Data Entity

我有一个名为 CollectedLeaf 的核心数据实体。

@interface CollectedLeaf :  NSManagedObject <LeafletRecognitionRequestDelegate>
{
    id <CollectedLeafDelegate> delegate_;
}

@property (nonatomic, assign) id <CollectedLeafDelegate> delegate;
@property (nonatomic, retain) NSString* leafID;
@property (nonatomic, retain) NSString* selectedSpecies;
@property (nonatomic, retain) NSString* latitude;
@property (nonatomic, retain) NSString* longitude;
@property (nonatomic, retain) NSString* altitude;
@property (nonatomic, retain) NSDate* collectedDate;
@property (nonatomic, retain) NSData * localImage;
@property (nonatomic, retain) LeafletURL* originalImageURL;
@property (nonatomic, retain) LeafletURL* segmentedImageURL;
@property (nonatomic, retain) Species* selectedSpeciesRel;
@property (nonatomic, retain) NSNumber* syncStatus;
@property (nonatomic, retain) NSDate* lastModified;
@property (nonatomic, retain) NSNumber* uploaded;
@property (nonatomic, retain) NSString* userDataset;
@property (nonatomic, retain) NSSet* CandidateSpecies;

当没有互联网连接时,我将使用 UIImagePickerController 拍摄的图像转换为 NSData 并存储到我的核心数据中。

if (_internetReachability == NotReachable){
//Internet Connection Not Available

    if(imageToUpload){
    //They just tried to upload photo
    self.originalImageView.image = [UIImage imageWithData:imageToUpload];

    /*Save photo to core data here*/
    NSManagedObjectContext* context = self.collectedLeaf.managedObjectContext;
    collectedLeaf.localImage = imageToUpload;
    NSError* error;
    [context save:&error];
    }

一旦有互联网连接,我就将图像上传到服务器。但是我知道在本地保存图片会占用大量内存,所以我想删除本地图片(只是赋给属性的值,而不是整个对象)。我可以用 collectedLeaf.localImage = nil 擦除图像吗?

您可以通过将 localImage 设置为 nil 来擦除图像。

// Upload the image to server

collectedLeaf.localImage = nil;

NSError* error;
[context save:&error];