0
- (IBAction)saveImage:(id)sender{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];


    NSManagedObject *image = [NSEntityDescription insertNewObjectForEntityForName:@"Image" inManagedObjectContext:context];
                              images.image = image;

                              [image setValue:tmpphoto forKey:@"img"];

                               CGSize size = tmpphoto.size;
                               CGFloat ratio = 0;
                               if (size.width > size.height) {
                                   ratio = 90.0 / size.width;
                               } else {
                                   ratio = 90.0 / size.height;
                               }
                               CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

                               UIGraphicsBeginImageContext(rect.size);
                               [tmpphoto drawInRect:rect];

                               images.imaget = UIGraphicsGetImageFromCurrentImageContext();

                               UIGraphicsEndImageContext();

}

私のエンティティ名は、属性 img(transformable)、imgcode(string) を持つ「Image」です。このイメージをコア データと共にデバイスに保存しようとしています。しかし、このエラーが発生しました。何が問題なのですか? 前もって感謝します...

エラー: 'NSInvalidArgumentException'、理由: 'Image' という名前の 2 つの異なるエンティティを持つモデルをマージできません''

4

3 に答える 3

0

画像プロパティのタイプをNSData変換可能ではなくに設定し、画像を取得する場合は、PNG表現を使用して取得します。

これを参照してください

于 2012-12-06T08:55:54.117 に答える
0

NSData * imageData = UIImagePNGRepresentation(tmpphoto); NSManagedObject * image = [NSEntityDescription insertNewObjectForEntityForName:@ "Image" inManagedObjectContext:context]; images.image = image;

[image setValue:imageData forKey:@"img"];

CGSize size = tmpphoto.size;
CGFloat ratio = 0;
if (size.width > size.height) {
        ratio = 44.0 / size.width;
} else {
    ratio = 44.0 / size.height;
}
CGRect rect = CGRectMake(0.0, 0.0, ratio * size.width, ratio * size.height);

UIGraphicsBeginImageContext(rect.size);
[tmpphoto drawInRect:rect];
images.imaget = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

私はこのように変化しました、そして私は同じ問題を抱えています。また、データモデルにはNSDataオプションがありません。画像用のBinarydataおよびTranformableオプションがあります。

于 2012-12-06T09:31:10.963 に答える
0
NSManagedObjectContext *context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    if (!currentPicture)
        self.currentPicture = (ImageList *)[NSEntityDescription insertNewObjectForEntityForName:@"ImageList" inManagedObjectContext:context];

    if (imageview.image)
    {
        float resize = 74.0;
        float actualWidth = imageview.image.size.width;
        float actualHeight = imageview.image.size.height;
        float divBy, newWidth, newHeight;
        if (actualWidth > actualHeight) {
            divBy = (actualWidth / resize);
            newWidth = resize;
            newHeight = (actualHeight / divBy);
        } else {
            divBy = (actualHeight / resize);
            newWidth = (actualWidth / divBy);
            newHeight = resize;
        }
        CGRect rect = CGRectMake(0.0, 0.0, newWidth, newHeight);
        UIGraphicsBeginImageContext(rect.size);
        [imageview.image drawInRect:rect];
        UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        NSData *smallImageData = UIImageJPEGRepresentation(smallImage, 1.0);
        [self.currentPicture setMyimage:smallImageData];
    }
    NSError *error;
    if (![context save:&error]){
        NSLog(@"Failed to add new picture with error: %@", [error domain]);}
    else
    {
        UIAlertView *AlertCampo = [[UIAlertView alloc] initWithTitle:@"
" message:@"Your image successfully saved into your device"
                                                            delegate:nil
                                                   cancelButtonTitle:@"OK"
                                                   otherButtonTitles:nil];
        [AlertCampo show];
    }

これで私は私の問題を解決しました。みんなに感謝します。

于 2012-12-11T07:56:36.803 に答える