2013年9月21日 星期六

CALayer setMask建立遮罩

//會讓Mask圖的黑色部份被顯示,白色部份則會透明
        //取得透明度遮罩的影像與大小
        UIImage *maskImage = [UIImage imageNamed:@"alphaMask.png"];
        CGRect rect = CGRectMake(0.0, 0.0, maskImage.size.width, maskImage.size.height);
       
        //製作一個透明遮罩的Layer
        CALayer *maskLayer = [[CALayer layer]retain];
        maskLayer.bounds = rect;
        maskLayer.contents = (id)maskImage.CGImage;
       
        //將imageView中的影像做透明度的遮罩
        [[middleViewController.view layer] setMask:maskLayer];
       
        [maskLayer release];

2013年9月16日 星期一

故意製造閃退的方法

abort();

KVC機制

The NSKeyValueCoding informal protocol defines a mechanism by which you can access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables. Thus, all of an object’s properties can be accessed in a consistent manner.
void changeName(Person *p, NSString *newName)
{
    // using the KVC accessor (getter) method
    NSString *originalName = [p valueForKey:@"name"];
    // using the KVC  accessor (setter) method.
    [p setValue:newName forKey:@"name"];
    NSLog(@"Changed %@'s name to: %@", originalName, newName);
}

2013年9月13日 星期五

為何objective c 要在.m檔中再@interface ViewControllor ()一次?是category嗎?

在XCode建立的UIViewController中,我們會在.m檔中看到
@interface ViewControllor ()
@end

在其中的宣告的內容,效果跟在.h中一樣,但是不會在.h中被其他人看到。
所以我們可以用來
  1. 修改.h中對property的設定,例如對外是宣告為readonly,在這再宣告回readwrite。
  2. 宣告primary method。

接下來是說明category,其實上面的class extention做法就可以看成是category的一個特例
下面可以看出其中的差別:在category中括號內部要放入你要作用的Class Name(上面提的沒有)
@interface NSObject(MySuperObject)
-(void) printRetainCount;
+(void) sayHello;
@end
 
category是讓你針對你繼承的class來新增你要的method,
或者是改寫其中某一個原來就有的method。 

手動建立UIViewController.xib的命名原則

如果取不同名稱,那要用initWithNibName:來初始化才會把它預設的view設為你的xib

如果.xib與.h.m取相同名稱,則可以只用init初始化,系統會主動把預設的view設為你的xib

如果.xib與.h.m取相同名稱,但在之後加上_iPhone或是_iPad(這裡注意大小寫要完全相同),系統會自動判斷裝置來把正確的xib設為預設的view

2013年9月12日 星期四

UIButton改text要用setTitle!

self.nameButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.nameButton addTarget:self
               action:@selector(nameButtonClicked:)
     forControlEvents:UIControlEventTouchUpInside];
[self.nameButton setTitle:@"Show Target Name" forState:UIControlStateNormal];
 self.nameButton.frame = CGRectMake(60.0, 20.0, 200.0, 80.0);
 [self.view addSubview:self.nameButton];

 //UIButton改text要用setTitle!
//而不是用self.nameButton.titleLabel.text,因為它是readonly

2013年9月11日 星期三

串接zxing 與 QCAR

一,在EAGLView中,加入下列function,目的是把qcarImage轉成uiimage
- (UIImage *)createUIImage:(const QCAR::Image *)qcarImage

{
   
    int width = qcarImage->getWidth();
   
    int height = qcarImage->getHeight();
   
    int bitsPerComponent = 8;
   
    int bitsPerPixel = QCAR::getBitsPerPixel(QCAR::RGB888);
   
    int bytesPerRow = qcarImage->getBufferWidth() * bitsPerPixel / bitsPerComponent;
   
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
   
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
   
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
   
   
   
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, qcarImage->getPixels(), QCAR::getBufferSize(width, height, QCAR::RGB888), NULL);
   
   
   
    CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
   
    UIImage *image = [[UIImage imageWithCGImage:imageRef] retain];
   
   
   
    CGDataProviderRelease(provider);
   
    CGColorSpaceRelease(colorSpaceRef);
   
    CGImageRelease(imageRef);
   
   
   
    return image;
   
}
二,在- (void)renderFrameQCAR中加入下列code
//ZXing
    ZxingQRcode* zxing = [ZxingQRcode shareInstance];
    QCAR::setFrameFormat(QCAR::RGB888, true);
    QCAR::Frame frame = state.getFrame();
    for (int i = 0; i < frame.getNumImages(); i++)
    {
        const QCAR::Image *qcarImage = frame.getImage(i);
        if (qcarImage->getFormat() == QCAR::RGB888)
        {
            UIImage *image = [self createUIImage:qcarImage];
            [zxing captureWithUIImage:image];
        }
    }

三,其中ZxingQRcode是我自己寫的一個class用來控制zxing做解碼之後的動作
這裡要注意的是,QCAR中和zxing中各有一個OverlayView,如果整合的時候不做處理,就會相衝突導致無法compile成功。
(Apple Mach-O Linker Error clang: error: linker command failed with exit code 1)


P.S.在第二項中setFrameFormat是為了用來取得RGB編碼的畫面,
關於畫面編碼QCAR提供了下面幾種選項
/// Pixel encoding types
enum PIXEL_FORMAT {
    UNKNOWN_FORMAT = 0,         ///< Unknown format - default pixel type for
                                ///< undefined images
    RGB565 = 1,                 ///< A color pixel stored in 2 bytes using 5
                                ///< bits for red, 6 bits for green and 5 bits
                                ///< for blue
    RGB888 = 2,                 ///< A color pixel stored in 3 bytes using
                                ///< 8 bits each
    GRAYSCALE = 4,              ///< A grayscale pixel stored in one byte
    YUV = 8,                    ///< A color pixel stored in 12 or more bits
                                ///< using Y, U and V planes
    RGBA8888 = 16,              ///< A color pixel stored in 32 bits using 8 bits
                                ///< each and an alpha channel.
};