如何使用目标c中的子视图获取uiimageview图像

时间:2014-04-30 06:35:21

标签: ios objective-c uiimageview subview

我想在UIIMageView中实现高级图片标记。目前图像标记工作正常。 我以编程方式将UIView(高级标记)添加为subview。因此,当我保存UIImageView图片时,SubViews没有被保存。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.imageView];


    UIView *rectangle = [[UIView alloc] initWithFrame:CGRectIntegral(CGRectMake(touchLocation.x,
                                                                                touchLocation.y,
                                                                                0,
                                                                                0))];
    rectangle.backgroundColor = [UIColor blackColor];
    rectangle.layer.cornerRadius = 10.0f;
    rectangle.layer.shadowColor = [UIColor blackColor].CGColor;
    rectangle.layer.shadowOffset = CGSizeMake(0.0f, 3.0f);
    rectangle.layer.shadowOpacity = 0.4;
    rectangle.layer.shadowRadius = 1.0;
    //rectangle.layer.shadowPath = rectangleShadowPath;

    JSBadgeView *badgeView = [[JSBadgeView alloc] initWithParentView:rectangle alignment:JSBadgeViewAlignmentTopRight];
    badgeView.badgeText = [NSString stringWithFormat:@"%d",i];
    i = i + 1;
    [self.imageView addSubview:rectangle];
    [self.imageView sendSubviewToBack:rectangle];

     [self saveImage:self.imageView.image WithImgName:@"abc"];
}

以下是我的高级标记图像。

enter image description here

那么如何用子视图保存UIImageView's图像(红色点标记)?

2 个答案:

答案 0 :(得分:1)

UIGraphicsBeginImageContext(CGSizeMake(self.imageView.frame.size.width, self.imageView.size.height));
CGContextRef context = UIGraphicsGetCurrentContext();
[self.imageView.layer renderInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

 [self saveImage:screenShot WithImgName:@"abc"];

答案 1 :(得分:0)

你可以通过截取UIImageView的截图来实现。

- (UIImage *)snapshot
{
    UIImage* snapShot = nil;
    UIGraphicsBeginImageContext(imageView.frame.size);
    {
        [imageView.layer renderInContext: UIGraphicsGetCurrentContext()];
        snapShot = UIGraphicsGetImageFromCurrentImageContext();

    }

    UIGraphicsEndImageContext();
    return snapShot;

}