iphone:截屏

介紹兩種圖片的處理方法
 
1,截獲當前的屏幕圖
 
2,從大圖中截圖一小塊圖
 
【1】當前的窗口是一個view
 
 CGSize mSize = self.frame.size;
 
  //這個size定義圖片的大小
 
  UIGraphicsBeginImageContext(mSize);
 
  //讀取當前畫布的內容
  [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
 
#if 0
    NSString *path = [NSHomeDirectory() stringByAppendingString:@"/tempImage.jpg"];
    NSLog(@"====%@",path);
    if ([UIImageJPEGRepresentation(tempImage, 1) writeToFile:path atomically:YES]) {
        NSLog(@"success");
    }
    else {
        NSLog(@"failed");
    }
#endif
 
 
 
【2】從大圖中截取一小圖
 
//大圖bigImage
 
//定義myImageRect,截圖的區域
 
CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);
 
UIImage* bigImage= [UIImage imageNamed:@"picture.png"];
 
CGImageRef imageRef = bigImage.CGImage;
CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);
 
 CGSize size;
 size.width = 57.0;
 size.height = 57.0;
 UIGraphicsBeginImageContext(size);
 CGContextRef context = UIGraphicsGetCurrentContext();
 CGContextDrawImage(context, myImageRect, subImageRef);
 UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
 UIGraphicsEndImageContext();
 
smallImage就是要截圖的大圖中的一部分。
 
附加:
將當前iPhone畫面另存爲圖片到Photos Album的方法非常簡單。主要步驟是首先獲取當前畫面的圖像數據,生成UIImage對象,然後用iPhone SDK中提供的API將UIImage對象保存到Photos Album就可以了。
 
代碼如下所示。
 
UIGraphicsBeginImageContext(currentView.bounds.size);
[currentView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
 
 
最後別忘了,還要在項目中添加QuartzCore.framework的引用以及在代碼中添加頭文件#import 。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章