1
我想寫OS X代碼到iOS的Objective-C。我有爲OS X編寫的代碼,我想在iOS中使用該代碼。我不知道如何爲iOS編寫下面的代碼。 我轉換下面的代碼,但我得到警告,只有一個可以請幫我在這轉換OS X代碼到iOS
OS X代碼
- (NSBitmapImageRep *) createImageRep:(NSSize)size
{
// Create an image rep that we can use as the backing store for the canvas.
// To keep things simple we'll use a 32-bit RGBA bitmap image.
int rowBytes = ((int)(ceil(size.width)) * 4 + 0x0000000F) & ~0x0000000F; // 16-byte aligned is good
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:nil
pixelsWide:size.width
pixelsHigh:size.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:rowBytes
bitsPerPixel:32];
// Paint on a white background so the user has something to start with.
NSGraphicsContext* imageContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:imageRep];
// "Focus" our image rep so the NSImage will use it to draw into
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:imageContext];
[[NSColor whiteColor] set];
[NSBezierPath fillRect: NSMakeRect(0, 0, size.width, size.height)];
[NSGraphicsContext restoreGraphicsState];
return imageRep;
}
IOS代碼
- (CGImageRef) createImageRep:(CGSize)size
{
int rowBytes = ((int)(ceil(size.width)) * 4 + 0x0000000F) & ~0x0000000F; // 16-byte aligned is good
CGImageRef maskCreate = CGImageMaskCreate(CGImageGetWidth((mImageRep)),
CGImageGetHeight((mImageRep)),
CGImageGetBitsPerComponent((mImageRep)),
CGImageGetBitsPerPixel((mImageRep)),
CGImageGetBytesPerRow((mImageRep)),
CGImageGetDataProvider((mImageRep)), NULL, false);
// Create an image rep that we can use as the backing store for the canvas.
// To keep things simple we'll use a 32-bit RGBA bitmap image.
// Paint on a white background so the user has something to start with.
CGImageRef masked = CGImageCreateWithMask(maskCreate, maskCreate);
CGImageRelease(maskCreate);
UIImage *maskedImage = [UIImage imageWithCGImage:masked];
UIGraphicsEndImageContext();
[[UIColor whiteColor] set];
[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, size.width, size.height)];
return maskedImage.CGImage;
}
我得到以下warning.Please幫我在這。
請先嚐試自己。如果您在試用過程中遇到問題並描述實際問題。 – shallowThought
我試過但得到的問題任何人都可以請告訴我如何在IOS下面的代碼編寫NSGraphicsContext * imageContext = [NSGraphicsContext graphicsContextWithBitmapImageRep:imageRep]; – Uttam
https://s23.postimg.org/3ma5br66z/Screen_Shot_2017_01_27_at_2_24_00_PM.png – Uttam