我是iOS编程的初学者。 我想逐帧显示屏幕上的图像。 但在我的源代码中,只有第一帧在屏幕上绘制并忽略了所有下一帧。 如何实时重新显示?
我的图像显示来源:
- viewDidLoad:init显示第一个屏幕
- ChangeImage:更改显示下一个屏幕
- setPixelColorR:G:B:X:Y ::更改屏幕上的像素颜色
<ContentViewController.h>
#import <UIKit/UIKit.h>
@interface ContentViewController : UIViewController {
IBOutlet UIImageView *svContent;
CGContextRef context;
size_t dataSize;
UInt8* initdata;
}
@property(nonatomic,retain) IBOutlet UIImageView *svContent;
-(IBAction)btnPhotosTouched;
- (void) ChangeImage;
- (void) setPixelColorR:(int)red G:(int)green B:(int)blue X:(int)x Y:(int)y;
@end
和
<ContentViewController.m>
#import "ContentViewController.h"
@implementation ContentViewController
@synthesize svContent;
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView;
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat width = screenSize.width;
CGFloat height = screenSize.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
size_t bitsPerComponent = 8;
size_t bytesPerPixel = 4;
size_t bytesPerRow = (width * bitsPerComponent * bytesPerPixel + 7) / 8;
dataSize = bytesPerRow * height;
initdata = malloc(sizeof(UInt8) * dataSize);
memset(initdata, 0, dataSize);
context = CGBitmapContextCreate(initdata, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
// a drawing for a test.
CGContextSetRGBFillColor (context, 1, 0, 0, 1);
CGContextFillRect (context, CGRectMake (0, 0, 200, 100 ));
CGContextSetRGBFillColor (context, 0, 0, 1, .5);
CGContextFillRect (context, CGRectMake (0, 0, 100, 200));
CGColorSpaceRelease(colorSpace);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *result = [[UIImage imageWithCGImage:imageRef] retain];
CGImageRelease(imageRef);
imageView = [[UIImageView alloc] initWithImage:result];
[svContent addSubview:imageView];
[imageView release];
// [self ChangeImage:initdata];
}
- (void) ChangeImage
{
UIImageView *imageView;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorSpaceRelease(colorSpace);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *result = [[UIImage imageWithCGImage:imageRef] retain];
CGImageRelease(imageRef);
imageView = [[UIImageView alloc] initWithImage:result];
[svContent addSubview:imageView];
[imageView release];
NSLog(@"Thread Image Change GO");
[svContent setNeedsDisplay];
[self.view setNeedsDisplay];
[super.view setNeedsDisplay];
}
- (void) setPixelColorR:(int)red G:(int)green B:(int)blue X:(int)x Y:(int)y
{
CGFloat r = (CGFloat)red / 255.0f;
CGFloat g = (CGFloat)green / 255.0f;
CGFloat b = (CGFloat)blue / 255.0f;
CGContextSetRGBFillColor(context, r, g, b, 1);
CGContextFillRect(context, CGRectMake(x, y, 1, 1));
}
.
.
.
@end
谢谢。