2013年7月31日 星期三

用程式抓取Xib(loading Xib programmatically)

if (iPad) {
            self.businessView = [[[NSBundle mainBundle] loadNibNamed:@"BusinessCardView~ipad" owner:self.view options:nil] objectAtIndex:0] ;
        }else{
            self.businessView = [[[NSBundle mainBundle] loadNibNamed:@"BusinessCardView" owner:self.view options:nil] objectAtIndex:0] ;
        }

2013年7月30日 星期二

新增iPad.xib

1,新增YourView~ipad.xib
2,把File's Owner的Custom Class改為YourViewController。
3,最重要的一步:對File's Owner按右鍵,把view跟你剛新增的View做連結。
4,把原來view上的元件copy過來,把該有的連結拉回去YourViewController.h裡。
5,最後,把初始化的地方改為判斷目前device來選用你的View
這裡是用我寫好的singleton為例
static QFunViewController* instance = nil;
+ (QFunViewController*) shareInstance{
    if (instance == nil) {
       
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
            instance = [[QFunViewController alloc]initWithNibName:@"QFunViewController" bundle:nil];
        else
            instance = [[QFunViewController alloc]initWithNibName:@"QFunViewController~ipad" bundle:nil];
    }
    return instance;
}

抓取目前螢幕尺寸

//抓觸控尺寸 
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
 
//抓目前的螢幕解析度
[UIScreen mainScreen] currentMode].size 

2013年7月29日 星期一

對UIImage做左右顛倒的轉換

#import <QuartzCore/QuartzCore.h>

+ (UIImage*)transformImageSymmetrically:(UIImage*)originalImage
{
    UIImage *transformedImage = originalImage;
    if (!isUsingBackCamera) {
 
        //建立一個暫存用的UIImageView
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];
 
        
        UIGraphicsBeginImageContext(tempImageView.frame.size);
        
        //這三行跟下面mark起來的七行,效果是一樣的,可以互相取代
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGAffineTransform flipHorizontal = CGAffineTransformMake(                                                               -1.0, 0.0, 0.0, 1.0, tempImageView.frame.size.width, 0.0 );
        CGContextConcatCTM(context, flipHorizontal);
//        CGImageRef imgRef = originalImage.CGImage;
//        CGFloat width = CGImageGetWidth(imgRef);
//        CGFloat height = CGImageGetHeight(imgRef);
//        CGAffineTransform transform = CGAffineTransformIdentity;  
//        transform = CGAffineTransformMakeTranslation(width,0.0);
//        transform = CGAffineTransformScale(transform, -1.0, 1.0);
//        CGContextConcatCTM(context, transform);
        [tempImageView.layer renderInContext:context];
        
        transformedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [tempImageView release];
        
    }
    return transformedImage;
}

2013年7月25日 星期四

從圖片網址轉成UIImage使用

NSURL *url = [NSURL URLWithString:@"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg"];
            NSData *data = [NSData dataWithContentsOfURL:url];
            UIImage *img  = [[UIImage alloc] initWithData:data];

CALayer轉成openGL的texture

貼圖會存在GLuint *tID;

//把UIView的CALayer畫成圖片
- (void)setOverlayLayer:(CALayer *)overlayLayer {
   
    UIImage* image = nil;
   
    UIGraphicsBeginImageContext(overlayLayer.frame.size);
    {
        [overlayLayer renderInContext: UIGraphicsGetCurrentContext()];
        image = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
   
    // Get the inner CGImage from the UIImage wrapper
    CGImageRef cgImage = image.CGImage;
   
    // Get the image size
    NSInteger width = CGImageGetWidth(cgImage);
    NSInteger height = CGImageGetHeight(cgImage);
   
    // Record the number of channels
    NSInteger channels = CGImageGetBitsPerPixel(cgImage)/CGImageGetBitsPerComponent(cgImage);
   
    // Generate a CFData object from the CGImage object (a CFData object represents an area of memory)
    CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
   
    unsigned char* pngData = new unsigned char[width * height * channels];
    const int rowSize = width * channels;
    const unsigned char* pixels = (unsigned char*)CFDataGetBytePtr(imageData);
   
    // Copy the row data from bottom to top
    for (int i = 0; i < height; ++i) {
        memcpy(pngData + rowSize * i, pixels + rowSize * (height - 1 - i), width * channels);
    }
   
    glClearColor(0.0f, 0.0f, 0.0f, QCAR::requiresAlpha() ? 0.0f : 1.0f);
   
    if (!trackingTIDSet) {
        glGenTextures(1, &tID);
    }

    glBindTexture(GL_TEXTURE_2D, tID);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   
   
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (GLvoid*)pngData);
   
   
    trackingTIDSet = YES;
    trackingTextureAvailable = YES;
   
    delete[] pngData;
    CFRelease(imageData);
   
    self.renderState = RS_NORMAL;
   
    NSLog(@"setOverlayer");
}

在做這個轉換之前,記得openGL只吃長寬為2的次方的貼圖
因為忽略這個基本觀念,整整花了二天debug得到這個教訓

2013年7月24日 星期三

圖片儲存本機加上callback

 
//你的照片放到theImage 
UIImageWriteToSavedPhotosAlbum(theImage,
   self, // send the message to 'self' when calling the callback
   @selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), // the selector to tell the method to call on completion
   NULL); // you generally won't need a contextInfo here
 
//執行callback
- (void)thisImage:(UIImage *)image hasBeenSavedInPhotoAlbumWithError:(NSError *)error usingContextInfo:(void*)ctxInfo {
    if (error) {
        // Do anything needed to handle the error or display it to the user
    } else {
        // .... do anything you want here to handle
        // .... when the image has been saved in the photo album
    }
}