cocoa touch - How do I programmatically draw to a display in iOS? -
i have method of drawing screen has many benefits application except small issue doesn't work... @ all.
i have ios program uiimageview widget , i'm trying programmatically draw looks black when run program. outlet declaration in header file:
@interface testviewcontroller : uiviewcontroller @property (weak, nonatomic) iboutlet uiimageview *imageview; @end ...and implementation:
@implementation testviewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. uigraphicsbeginimagecontextwithoptions(cgsizemake(400, 400), yes, 0.0); cgcontextref context = uigraphicsgetcurrentcontext(); cgfloat colour[] = { 1, 0, 0, 1 }; cgcontextsetfillcolor(context, colour); cgcontextfillrect(context, cgrectmake(0, 0, 400, 400)); self.imageview.image = uigraphicsgetimagefromcurrentimagecontext(); [self.imageview setneedsdisplay]; uigraphicsendimagecontext(); } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end testviewcontroller delegate view controller , imageview outlet uiimageview widget. try draw 400 x 400 red box image , assign image widget. call setneedsdisplay measure.
what doing wrong? thank you!
these lines problem:
cgfloat colour[] = { 1, 0, 0, 1 }; cgcontextsetfillcolor(context, colour); delete them. instead, set fill color way:
cgcontextsetfillcolorwithcolor(context, [uicolor redcolor].cgcolor); the reason issue failing create color space. needed have called cgcontextsetfillcolorspace, , failed so. that's needed if use cgcontextsetfillcolor. it's deprecated, don't use it. use cgcontextsetfillcolorwithcolor, docs recommend. takes care of color space issue you.
Comments
Post a Comment