标签: cgimage

从NSData显示NSImage

我的目标是在视图中显示图像.考虑到我有一个:

  • IBOutlet NSImageView*图像用于显示我的图像
  • NSData*imageData用于读取图像文件
  • NSImage*imageView

在imageData中存储了图像(我使用了initWithContentsOfFile方法).

现在,如果我用以下内容初始化imageView:

NSImage *imageView = [[NSImage alloc] initWithContentsOfFile:path];
Run Code Online (Sandbox Code Playgroud)

我可以正确地看到图像的渲染,但是这样我就从文件系统中读取了两次.

如果我尝试:

NSImage *imageView = [[NSImage alloc] initWithData:imageData];
Run Code Online (Sandbox Code Playgroud)

显示的图像非常小..像拇指

Whit CGImageRef:

CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
CGImageRef imageRef= CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
NSImage *imageView = [[NSImage alloc] initWithCGImage:imageRef size:NSZeroSize];
Run Code Online (Sandbox Code Playgroud)

图像仍然渲染得太小.

如何以原始分辨率显示图像?

objective-c nsview nsimage cgimage nsdata

4
推荐指数
1
解决办法
6766
查看次数

CGImage会产生大量脏内存,导致应用程序崩溃

我尝试通过填写自己的数据来创建UIImage.到目前为止一切正常.但是,如果我尝试多次调用此函数,它似乎会填满内存,直到应用程序崩溃.使用VM Tracker我发现脏内存增长到328MB,当应用程序崩溃时,290MB是CG图像内存.

我多次调用我的函数循环,从而启用ARC.图像非常大,但这应该不是问题,因为它适用于29次迭代.据我所知,脏内存应该再次被应用程序重用.是对的吗?那么为什么它会填满我的记忆呢?我该如何避免这个问题呢?

for(int i = 0; i < 1000; ++i) {
    UIImage *img = [self createDummyImage:CGSizeMake(2000, 1600)];
}
Run Code Online (Sandbox Code Playgroud)

创建虚拟UIImage的功能:

- (UIImage*)createDummyImage:(CGSize)size
{
    unsigned char *rawData = (unsigned char*)malloc(size.width*size.height*4);

    // fill in rawData (logic to create checkerboard)

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host;
    CGContextRef contextRef = CGBitmapContextCreate(rawData, size.width, size.height, 8, 4*size.width, colorSpaceRef, bitmapInfo);
    CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);

    CGColorSpaceRelease(colorSpaceRef);
    CGContextRelease(contextRef);
    free(rawData);

    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return image;
}
Run Code Online (Sandbox Code Playgroud)

hooleyhoop的工作解决方案

将函数调用放入自动释放池中.

for (int i = 0; …
Run Code Online (Sandbox Code Playgroud)

iphone memory-management cgimage ios5

4
推荐指数
1
解决办法
1576
查看次数

自从更新到Swift 1.2以来,Dictionary现在给出错误'不能转换为BooleanLiteralConvertible'

我只是围绕Swift - 然后来了Swift 1.2(打破我的工作代码)!

我有一个基于NSHipster的代码示例的函数 - CGImageSourceCreateThumbnailAtIndex.

我以前工作的代码是:

import ImageIO

func processImage(jpgImagePath: String, thumbSize: CGSize) {

    if let path = NSBundle.mainBundle().pathForResource(jpgImagePath, ofType: "") {
        if let imageURL = NSURL(fileURLWithPath: path) {
            if let imageSource = CGImageSourceCreateWithURL(imageURL, nil) {

                let maxSize = max(thumbSize.width, thumbSize.height) / 2.0

                let options = [
                    kCGImageSourceThumbnailMaxPixelSize: maxSize,
                    kCGImageSourceCreateThumbnailFromImageIfAbsent: true
                ]

                let scaledImage = UIImage(CGImage: CGImageSourceCreateThumbnailAtIndex(imageSource, 0, options))

                // do other stuff
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

从Swift 1.2开始,编译器提供了两个与options字典相关的错误:

  1. 如果没有更多的上下文,表达的类型是不明确的
  2. '_'不能转换为'BooleanLiteralConvertible' (在ref为'true'值时) …

dictionary cgimage swift

4
推荐指数
1
解决办法
1426
查看次数

使用 UIImages 创建 gif

我指的是这篇文章。我正在尝试使用屏幕截图创建的图像制作一个 gif 文件。我正在使用计时器来创建屏幕快照,以便获得可用于创建 gif 所需的帧数。我每 0.1 秒拍摄一次快照(稍后我将在 3 秒后结束此计时器)。

这是我的 UIView 快照的代码:

-(void)recordScreen{

   self.timer= [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(takeSnapShot) userInfo:nil repeats:YES];

}

-(void)takeSnapShot{

    //capture the screenshot of the uiimageview and save it in camera roll
    UIGraphicsBeginImageContext(self.drawView.frame.size);
    [self.drawView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}
Run Code Online (Sandbox Code Playgroud)

我所指的帖子显示了创建 gif 的辅助函数。我不确定应该如何将图像传递给辅助函数。这是我尝试过的:

我尝试修改这部分:

 static NSUInteger kFrameCount = 10;
 for (NSUInteger i = 0; i < kFrameCount; i++) {
        @autoreleasepool {
            UIImage *image = [self takeSnaphot];
            CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这将创建包含 10 帧我的 …

objective-c gif cgimage ios

4
推荐指数
1
解决办法
2543
查看次数

将 CGImage 转换为 NSImage Swift

我正在尝试在 NSImageView 框中显示 CGImage,但在将 CGImage 转换为 NSImage 时遇到问题。我该如何应对这种情况?谢谢你!

nsimage cgimage swift

4
推荐指数
1
解决办法
3728
查看次数

在Swift 3中绘制CGImage的直方图

vImageHistogramCalculation_ARGB8888尝试将库从Swift 2转换为Swift 3版本时,我遇到了方法问题.问题是该方法仅接受"histogram"参数

UnsafeMutablePointer<UnsafeMutablePointer<T>?> 
Run Code Online (Sandbox Code Playgroud)

但是Swift 3的构造

let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>>(mutating: rgba)  
Run Code Online (Sandbox Code Playgroud)

返回unwrapped值,所以我无法将其强制转换为正确的类型.

编译器错误是:

:无法使用类型'(mutating:[UnsafeMutablePointer])'的参数列表调用类型'UnsafeMutablePointer?>'的初始值设定项

你有什么想法吗?我试着添加"?" 到直方图常数,但后来我收到错误:

'init'不可用:使用'withMemoryRebound(to:capacity:_)'临时查看内存作为另一种布局兼容类型.

有一些编译器建议,但我不知道如何使用它.

histogram cgimage ios accelerate-framework swift

4
推荐指数
1
解决办法
992
查看次数

AVCAPTURE 图像方向

在此处输入图片说明我有一个允许用户拍照的视图控制器。我将 avcapture 边界设置为屏幕上视图的边界。

在此视图上方,我有一个集合视图。因此用户可以捕获多张图片,然后将它们添加到上面的集合视图中。

我在上面的预览中出现正确的方向时遇到了问题。

代码如下:

@IBOutlet weak var imagePreviews: UICollectionView!
@IBOutlet weak var imgPreview: UIView!

var session: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?
var images: [UIImage] = [UIImage]()

var isLandscapeLeft     : Bool = false
var isLandscapeRight    : Bool = false
var isPortrait          : Bool = false
var isPortraitUpsideDown: Bool = false

@IBAction func capture(_ sender: UIButton)
    {
    if let videoConnection = stillImageOutput?.connection(withMediaType: AVMediaTypeVideo)
            {
                stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) in
                    if sampleBuffer != nil {
                        if let …
Run Code Online (Sandbox Code Playgroud)

uiimage cgimage ios avcapturesession swift

4
推荐指数
1
解决办法
1772
查看次数

iPhone 4相机拍摄的照片分辨率是多少?

在规格中,

iPhone 4屏幕分辨率和像素密度*iPhone 4的屏幕分辨率为960×640像素,是之前iPhone型号的两倍

众所周知,当我们这样编码时,

CGImageRef screenImage = UIGetScreenImage();
CGRect fullRect = [[UIScreen mainScreen] applicationFrame];
CGImageRef saveCGImage = CGImageCreateWithImageInRect(screenImage, fullRect);
Run Code Online (Sandbox Code Playgroud)

saveCGImage将有大小(320,480),我的问题是iPhone 4怎么样?那是(640,960)?

另一个问题是关于打开Photo.app的拇指视图中的黑色图像,如果像这样编码,

CGImageRef screenImage = UIGetScreenImage();

CGImageRef saveCGImage = CGImageCreateWithImageInRect(screenImage, CGRectMake(0,0,320,460));  // please note, I used 460 instead of 480
Run Code Online (Sandbox Code Playgroud)

问题是当打开"Photo.app"时,在拇指视图中,这些图像被视为黑色,当点击它以查看细节时,这是可以的.现在解决这个问题的任何方法?

谢谢你的时间.

更新问题:

当您调用UIGetScreenImage()来捕获iPhone 4中的屏幕时,它是否也是320x480?

iphone image cgimage

3
推荐指数
1
解决办法
3万
查看次数

iOS Retina显示屏蔽错误

我目前正在使用两个图像作为我构建的菜单.前一段时间我正在使用这个代码用于普通的显示系统,它工作正常,视网膜显示器我在CGImageRef上有一些问题,在背景显示的凹陷上创建正确的蒙版图像.我尝试使用图像扩展名导入视网膜图像.图像使用以下方式提供:

[UIImage imageNamed:@"filename.png"]
Run Code Online (Sandbox Code Playgroud)

我提供了带有filename.png和filename@2x.png名称的标准和视网膜图像.

选择所选区域的遮罩时会出现问题.代码适用于较低分辨率的资源和高分辨率的主资源,但是当我使用时

CGImageCreateWithImageInRect
Run Code Online (Sandbox Code Playgroud)

并指定我要在其中创建图像的矩形,图像的比例增加意味着主按钮的分辨率很好,但返回并叠加在按钮按下的图像不是正确的分辨率,但奇怪地缩放到两次像素密度,看起来很糟糕.

我试过了两个

    UIImage *img2 = [UIImage imageWithCGImage:cgImg scale:[img scale] orientation:[img imageOrientation]];
    UIImage *scaledImage = [UIImage imageWithCGImage:[img2 CGImage] scale:4.0 orientation:UIImageOrientationUp];
Run Code Online (Sandbox Code Playgroud)

当我拍摄图像和drawInRect时,我似乎无处可去:(选定的矩形)

我现在已经把头发撕掉了大约2个小时,似乎找不到合适的解决方案,有没有人有任何想法?

iphone cgimage ios4 retina-display

3
推荐指数
1
解决办法
2224
查看次数

我可以在UIImageView中编辑UIImage的Alpha Mask而不必移动太多内存吗?

我想拍摄一张图片(画笔)并将其绘制成一个显示的图像.我只想影响该图像的alpha,我需要稍后导出它.

从我所看到的情况来看,大多数方向只是真正进入一些看起来很昂贵的操作,而这些操作并没有成功.即他们建议你绘制一个屏幕外的上下文,创建一个掩码的CGImage,并在每次刷子应用时创建一个CGImageWithMask.

我已经知道这是昂贵的,因为即使只是这样做并进入上下文对于iPhone来说相当粗糙.

我想做的是获取UIImageView的UIImage,并直接操作它的alpha通道.我也不是逐像素地做这件事,而是用一个较大的(20px半径)刷子,它具有自己的柔软度.

iphone quartz-graphics uiimage cgimage ios

3
推荐指数
1
解决办法
1832
查看次数