【发布时间】:2015-04-29 17:33:45
【问题描述】:
我有一个问题:我正在创建一个从方法返回的UIView,这部分很好,但我注意到,当我将子层添加到其中一个子视图时,这些层重叠层次结构中较高的子视图(textView 和 imageView)添加到 testViewCopy 的子层出现在这些子视图的顶部,它们不应该出现。我不知道这里发生了什么。
代码:
- (void)makeShareImages
{
UIView *shareView = [self shareView];
UIView *testViewCopy = [shareView viewWithTag:0];
NSUInteger currentIndex = 1;
for (NSDictionary *sub in self.array)
{
NSArray *lastArray = [sub objectForKey:@"LastArray"];
for (NSDictionary *dict in lastArray)
{
@autoreleasepool
{
currentIndex ++;
CircleLayer *layer = [[CircleLayer alloc]init];
layer.portrait = [[dict objectForKey:@"Portrait"]boolValue];
layer.frame = testViewCopy.bounds;
[testViewCopy.layer addSublayer:layer];
NSData *frameData = [self getSnapshotDataFromView:shareView];
NSString *savePath = [NSString stringWithFormat:@"%@/%lu.png",somePath,(unsigned long)currentIndex];
[frameData writeToFile:savePath options:0 error:nil];
}
}
}
}
- (UIView *)shareView
{
UIColor *bgColor = self.containerView.backgroundColor;
CGSize size = self.containerView.bounds.size;
UIView *viewToShare = [[UIView alloc]init];
viewToShare.backgroundColor = bgColor;
viewToShare.layer.cornerRadius = 6.0;
viewToShare.layer.masksToBounds = YES;
UIView *testViewCopy = [[UIView alloc]init];
testViewCopy.backgroundColor = [UIColor clearColor];
testViewCopy.contentMode = UIViewContentModeScaleAspectFit;
testViewCopy.layer.masksToBounds = YES;
testViewCopy.tag = 0;
UITextView *textViewCopy = [[UITextView alloc]init];
textViewCopy.backgroundColor = [UIColor clearColor];
textViewCopy.tag = 1;
textViewCopy.textContainerInset = self.textView.textContainerInset;
UIImageView *profileImageViewCopy = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 32, 32)];
profileImageViewCopy.contentMode = UIViewContentModeScaleAspectFit;
profileImageViewCopy.layer.masksToBounds = YES;
profileImageViewCopy.image = [self profileImage];
profileImageViewCopy.tag = 2;
profileImageViewCopy.layer.cornerRadius = profileImageViewCopy.frame.size.width / 2.0;
viewToShare.frame = CGRectMake(0, 0, size.width, size.height);
testViewCopy.frame = CGRectMake(0, 0, size.width, size.width);
textViewCopy.frame = CGRectMake(0, 0, size.width, size.height);
NSAttributedString *attributedStringCopy = [[NSAttributedString alloc]initWithAttributedString:self.textView.attributedText];
textViewCopy.attributedText = attributedStringCopy;
[viewToShare addSubview:testViewCopy];
[viewToShare addSubview:textViewCopy];
[viewToShare addSubview:profileImageViewCopy];
return viewToShare;
}
- (NSData *)getSnapshotDataFromView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *snapShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return UIImagePNGRepresentation(snapShot);
}
【问题讨论】:
标签: ios objective-c uiview