2012-12-06 39 views
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(變形),imgcode(串),我嘗試這個圖像保存到我的設備與核心data.But我有這個錯誤,是什麼可以是問題嗎?預先感謝您...保存圖像設備與coredata

錯誤:「NSInvalidArgumentException」的,理由是:圖片「

+0

我不能評論你的問題的核心數據方面,但有兩點意見:1.你可以使用['UIImagePNGRepresentation'](http://developer.apple.com/library/ios/documentation/uikit/reference /UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIImagePNGRepresentation)或'UIImageJPEGRepresentation'來檢索圖像,而不是圖形上下文方法; 2.圖像(除非非常小,小於100kb)不適合存儲在CoreData中。 – Rob

+0

並且當我評論這兩行時 AppDelegate * appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext * context = [appDelegate managedObjectContext]; 而不是這些行 NSManagedObject * image = [NSEntityDescription insertNewObjectForEntityForName:@「Image」inManagedObjectContext:self.managedObjectContext];我做這個我的錯誤正在改變爲entityForName:nil不是合法的NSManagedObjectContext參數搜索實體名稱'圖像'??? – santa

回答

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]; 
    } 

有了這個,我已經解決了我的problem.Thanks爲大家NSData的選項there.Binarydata和Tranformable選項。

0

集型的你像財產NSData而不是變形」無法與命名兩個不同的實體合併模型「和當你想獲得圖像時,請使用PNG表示。

see this

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(); 

我已經改變了這個,我也有同樣的問題。而也是在數據模型我們沒有,我們有圖像