【问题标题】:Objective C manipulate imageObjective C 操作图像
【发布时间】:2017-04-16 03:18:26
【问题描述】:

我想处理图像和随机颜色。我试图用像素旋转 180 度但失败了。我不想使用 UIImageView 旋转因为我不会只旋转图像。我想为所欲为。

编辑:这是错误的运营商。我不知道为什么我使用 % 而不是 / 。无论如何,我希望这段代码可以帮助某人(它有效)。

- (IBAction)shuffleImage:(id)sender {

[self calculateRGBAsAndChangePixels:self.imageView.image atX:0 andY:0];

}

-(void)calculateRGBAsAndChangePixels:(UIImage*)image atX:(int)x andY:(int)y

{


NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * image.size.width;
NSUInteger bitsPerComponent = 8;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bmContext = CGBitmapContextCreate(NULL, image.size.width, image.size.height, bitsPerComponent,bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, image.size.width, image.size.height}, image.CGImage);

UInt8* data = (UInt8*)CGBitmapContextGetData(bmContext);

const size_t bitmapByteCount = bytesPerRow * image.size.height;
NSMutableArray *reds = [[NSMutableArray alloc] init];
NSMutableArray *greens = [[NSMutableArray alloc] init];
NSMutableArray *blues = [[NSMutableArray alloc] init];



for (size_t i = 0; i < bitmapByteCount; i += 4)
{
    [reds addObject:[NSNumber numberWithInt:data[i]]];
    [greens addObject:[NSNumber numberWithInt:data[i+1]]];
    [blues addObject:[NSNumber numberWithInt:data[i+2]]];

}

for (size_t i = 0; i < bitmapByteCount; i += 4)
{

    data[i] = [[reds objectAtIndex:reds.count-i%4-1] integerValue];
    data[i+1] = [[greens objectAtIndex:greens.count-i%4-1] integerValue];
    data[i+2] = [[blues objectAtIndex:blues.count-i%4-1] integerValue];

}

CGImageRef newImage = CGBitmapContextCreateImage(bmContext);
UIImage *imageView = [[UIImage alloc] initWithCGImage:newImage];
self.imageView.image = imageView;
}

【问题讨论】:

    标签: ios objective-c xcode image uiimageview


    【解决方案1】:

    因此,您希望实际操作原始像素数据。那就看看吧:

    Getting the pixel data from a CGImage object

    它适用于 MacOS,但也应该适用于 iOS。

    【讨论】:

    • 我对该链接做了同样的事情,但仍然不知道为什么第二张照片是一种颜色。
    【解决方案2】:

    假设您想让图像倒置(将其旋转 180 度)而不是镜像它,我在 another question 上找到了一些可能对您有所帮助的相关代码:

    static inline double radians (double degrees) {return degrees * M_PI/180;}
    UIImage* rotate(UIImage* src, UIImageOrientation orientation)
    {
        UIGraphicsBeginImageContext(src.size);
    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        if (orientation == UIImageOrientationRight) {
            CGContextRotateCTM (context, radians(90));
        } else if (orientation == UIImageOrientationLeft) {
            CGContextRotateCTM (context, radians(-90));
        } else if (orientation == UIImageOrientationDown) {
            // NOTHING
        } else if (orientation == UIImageOrientationUp) {
            CGContextRotateCTM (context, radians(90));
        }
    
        [src drawAtPoint:CGPointMake(0, 0)];
    
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    

    如果您尝试镜像图像,this question 中的此代码示例可能会有所帮助:

    UIImage* sourceImage = [UIImage imageNamed:@"whatever.png"];
    
    UIImage* flippedImage = [UIImage imageWithCGImage:sourceImage.CGImage 
                                                scale:sourceImage.scale
                                          orientation:UIImageOrientationUpMirrored]; 
    

    【讨论】:

    • 我想做这样的事情,但要一个一个地处理像素。
    • 诚然,我对这个过程不是很熟悉,但this link 应该有助于从 CGImage 获取像素数据。得到像素数据后,
    • 糟糕,在我完成输入之前发送了评论,我不知道如何编辑它......无论如何,在你获得像素数据后,你会想要执行 @987654324 @ 在该数据上,它应该反映它。
    • 我对那个链接做了同样的事情,但仍然不知道为什么第二张照片是一种颜色。
    猜你喜欢
    • 2016-11-03
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2011-04-23
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多