2010-01-12 69 views
2

我使用UIPickerController從iPhone捕獲圖像,然後如何以編程方式將其保存爲特定名稱,並稍後(通過使用名稱)調用圖像,是否有任何可能性來更改圖像大小,例如。從100×100像素到50×50像素如何保存具有特定名稱的UIImagePicker圖像

感謝

回答

7

下面的代碼段將所述圖像保存到文件中:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage *anImage = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    NSData *imageData = UIImageJPEGRepresentation(anImage, 1.0); 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *path = [paths objectAtIndex:0]; 
    NSString *tmpPathToFile = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/specificImagedName.jpg", path]]; 

    if([imageData writeToFile:tmpPathToFile atomically:YES]){ 
      //Write was successful. 
    } 
} 

然後從文件中調出圖像:

NSData *imageData = [NSData dataWithContentsOfFile:tmpPathToFile]; 
[UIImage imageWithData:imageData]; 

最後,this post有一個方法可用,您可以將它合併到項目中以調整圖像大小。

相關問題