将图层内容设置为 CALayer 子类内的 UIImage - 不起作用
我试图显示从 CALayer 子类中的 UIImagePickerController 选择的 UIImage 并将其添加到我的超级视图中,但我无法让它工作。
这就是我从 UIImagePickerController 获取图像的方法,这似乎工作得很好:
- (IBAction)importImage:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[popover setPopoverContentSize:CGSizeMake(320,320)];
[popover presentPopoverFromRect:importBackgroundButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popoverController = popover;
[imagePickerController release];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:YES];
UIImage* img = [info objectForKey:UIImagePickerControllerOriginalImage];
[[context currentDrawing] setBkgImage:img];
}
这是我的 CALayer 子类,其中包含要显示的 UIImage:
@interface UI_BackgroundImageLayer : CALayer
{
//This is for the Import image as background feature
UIImage* _bkgImg;
}
@property (nonatomic, retain) UIImage* _bkgImg;
-(void)update:(UIImage*)src;
@end
////
@implementation UI_BackgroundImageLayer
- (id)init
{
self = [ super init ];
// [self resetTransform ];
_bkgImg = nil;
return self;
}
-(void)update:(UIImage*)src
{
_bkgImg = [[UIImage alloc] initWithCGImage:src.CGImage];
self.contentsGravity = kCAGravityResizeAspect;
self.contents = (id)_bkgImg.CGImage;
}
@end
我更新了当用户选择另一个图像时 CALayer !
CALayer 从一开始就添加到超级视图中,如下所示:
[[_superView layer] insertSublayer:context._imgLayer atIndex:0];
它被插入到索引 0 处,因为它是背景图像,因此它必须位于其他 UI 元素和绘图下方。
所有这一切对我来说似乎都很好,但屏幕上什么也没有出现......
I'm trying to display a UIImage selected from a UIImagePickerController inside a subclass of CALayer and add it to my superview but I can't get it to work.
This is how I Get the Image from the UIImagePickerController, which seems to work good :
- (IBAction)importImage:(id)sender
{
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
[popover setPopoverContentSize:CGSizeMake(320,320)];
[popover presentPopoverFromRect:importBackgroundButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popoverController = popover;
[imagePickerController release];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:YES];
UIImage* img = [info objectForKey:UIImagePickerControllerOriginalImage];
[[context currentDrawing] setBkgImage:img];
}
This is my subclass of CALayer which contains the UIImage to display :
@interface UI_BackgroundImageLayer : CALayer
{
//This is for the Import image as background feature
UIImage* _bkgImg;
}
@property (nonatomic, retain) UIImage* _bkgImg;
-(void)update:(UIImage*)src;
@end
////
@implementation UI_BackgroundImageLayer
- (id)init
{
self = [ super init ];
// [self resetTransform ];
_bkgImg = nil;
return self;
}
-(void)update:(UIImage*)src
{
_bkgImg = [[UIImage alloc] initWithCGImage:src.CGImage];
self.contentsGravity = kCAGravityResizeAspect;
self.contents = (id)_bkgImg.CGImage;
}
@end
I update the content of the CALayer when the user select another image !
The CALayer is added to the superview from start like this :
[[_superView layer] insertSublayer:context._imgLayer atIndex:0];
It's inserted at index 0 because it's meant to be a background image so it must be under other UI elements and drawings.
All of this seems good to me, but nothing appears on the screen ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是 XCode v4.2 吗?如果是这样,这可能与您相关,也可能不相关,但值得一看。我遇到了与您完全相同的问题,CALayer 子类不响应通常的显示方法(如 setCornerRadius、setBackgroundColor 等...)。试试这个;在您的托管视图中,在实例化 VIEW 之后,放置以下行;
[自行设置背景颜色[UIColorclearColor]];
您可能会发现从那时起,您的图层子类就完美地工作了。
我的视图子类代码如下所示;
似乎图层子类在其托管视图“确认”它的存在之前不会响应。我花了几天时间进行调试,但现在我的所有代码都可以完美运行。
希望这有帮助。
维维
Are you using XCode v4.2? If so, this may be relevant to you or it may not but it's worth a look. I've had exacty the same problem as you with CALayer subclasses not responding to the usual display methods (like setCornerRadius, setBackgroundColor etc...). Try this; in your hosting view, just after you've instanced the VIEW put the following line;
[self setBackgroundColor [UIColor clearColor]];
You may well find that from that point on, your layer subclass works perfectly.
My view subclass code looks like this;
It seems that the layer subclass does not respond until its hosting view has 'acknowledged' that it's there. It took me days to debug but now all my code works perfectly.
Hope this helps.
V.V.