Xuni for IOS のドキュメント
画像のエクスポート

Xuni FlexPie を使用して、円グラフを画像の形式でエクスポートし、それを iOS デバイスのカメラロールに保存したり、必要に応じて好きな場所を選択して保存できます。iPhone や iPad でとるスクリーンショットとまったく同様に機能します。次の例では、スナップショットボタンを FlexPie の上端に追加します。ユーザーは、ボタンをタップしてチャートをギャラリーに保存できます。

サンプルコード

override func viewDidLoad() 
{
     super.viewDidLoad()
     self.edgesForExtendedLayout = UIRectEdge.None
      // ビューを読み込んだ後の追加セットアップを行います。
     var pieData = NSMutableArray()
     pieData = PieData.demoData()
     pieChart.binding = "value"
     pieChart.bindingName = "name"
     pieChart.itemsSource = pieData
     pieChart.tooltip.isVisible = true
       
     snapshotButton = UIButton(type: UIButtonType.System)
     snapshotButton.setTitle(NSLocalizedString("Take a snapshot", comment: "Take a snapshot"), forState: UIControlState.Normal)
     snapshotButton.addTarget(self, action: "snapshotButtonClicked", forControlEvents: UIControlEvents.TouchUpInside)

     self.view.addSubview(pieChart)
     self.view.addSubview(snapshotButton)
    }
    override func viewDidLayoutSubviews() 
  {
        super.viewDidLayoutSubviews()
        
        pieChart.frame = CGRectMake(5, self.view.bounds.size.height/3, self.view.bounds.size.width-10, self.view.bounds.size.height/2)
        snapshotButton.frame = CGRectMake(0, 65, self.view.bounds.size.width, 50);
    }    
    func snapshotButtonClicked()
  {
        let image = UIImage(data: pieChart.getImage())
        UIImageWriteToSavedPhotosAlbum(image!, self, "imageSavedToPhotoAlbum:error:contextInfo:", nil)
    }
    func imageSavedToPhotoAlbum(image: UIImage!, error: NSError!, contextInfo: UnsafePointer<Void>){
        var message = String()
        var title = String()
        if(error == nil){
            title = NSLocalizedString("Success", comment: "Success")
            message = NSLocalizedString("Image was saved to Camera Roll successfully", comment: "Image was saved to Camera Roll successfully")
        }
        else{
            title = NSLocalizedString("Failure", comment: "Failure")
            message = error.description
        }
        let alert = UIAlertView(title: title, message: message, delegate: nil, cancelButtonTitle: NSLocalizedString("OK", comment: "OK"))
        alert.show();
    }
- (void)viewDidLoad {
    [super viewDidLoad];
    // ビューを読み込んだ後の追加セットアップを行います。
    
    UIButton *snapshotButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [snapshotButton setTitle:@"Take a snapshot" forState:UIControlStateNormal];
    [snapshotButton addTarget:self action:@selector(snapshotButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    FlexPie *pieChart = [[FlexPie alloc] init];
    NSMutableArray *pieData = [PieChartData demoData];
    pieChart.binding = @"value";
    pieChart.bindingName = @"name";
    pieChart.itemsSource = pieData;
    pieChart.tooltip.visible = true;
    pieChart.tag = 1;
    snapshotButton.tag = 2;
    [self.view addSubview:snapshotButton];
    [self.view addSubview:pieChart];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    FlexPie *pieChart= (FlexPie*)[self.view viewWithTag:1];
    UIButton *snapshotButton = (UIButton*)[self.view viewWithTag:2];
    pieChart.frame = CGRectMake(5, self.view.bounds.size.height/3, self.view.bounds.size.width-10, self.view.bounds.size.height/2);
    snapshotButton.frame = CGRectMake(0, 65, self.view.bounds.size.width, 50);
}
-(void)snapshotButtonClicked{
    FlexPie *pie = (FlexPie*)[self.view viewWithTag:1];
    UIImage *image = [[UIImage alloc] init];
    image = [UIImage imageWithData:[pie getImage]];
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotoAlbum:finishedSavingWithError:contextInfo:), nil);
}
-(void)imageSavedToPhotoAlbum:(UIImage *)image finishedSavingWithError: (NSError *)error contextInfo:(void *)contextInfo{
    NSString *message;
    NSString *title;
    if (!error) {
        title = @"Success";
        message = @"Image was saved to Camera Roll succesfully";
    }
    else{
        title = @"Failure";
        message = [error description];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

@end

 

 


Copyright © GrapeCity inc. All rights reserved.