为什么有些 UIimages 不显示在 iphone 中

发布于 2024-12-05 12:23:59 字数 4158 浏览 1 评论 0原文

嗨,我目前正在 ios 4.3 上使用 Objective C 开发一个小应用程序 作为应用程序的一部分,我需要操作从网上下载的图像。 下面的代码显示了丢失的图像:(

原始代码在一个类中,但我只是将其放在一起作为测试场景,以便可以轻松复制粘贴)

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadImage:@"http://www.night-net.net/images/ms/microsoft_vista_home_basic.jpg"];
    [self getCroped:CGRectMake(10, 50, 80, 160)];
    [self getCroped:CGRectMake(90, 50, 80, 80)];
    [self getCroped:CGRectMake(90, 130, 40, 80)];
    [self getCroped:CGRectMake(130, 130, 40, 40)];
    [self getCroped:CGRectMake(130, 170, 40, 40)];

}
-(void) loadImage : (NSString*) url
{
     _data = [NSData dataWithContentsOfURL: 
              [NSURL URLWithString: url]];
}
-(UIImageView*) getCroped:(CGRect) imageSize{
    UIImage *temp = [[UIImage alloc] initWithData:_data];
    UIImage *myImage = [self resizedImage:temp and:CGSizeMake(160,160) interpolationQuality:kCGInterpolationHigh];
    UIImage *image = [self croppedImage:myImage and:imageSize];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = image;
    imageView.frame = imageSize;
    [[self view] addSubview:imageView];

    return imageView;
}
- (UIImage *)croppedImage:(UIImage*) image and: (CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;
}


- (UIImage *)resizedImage:(UIImage*) image and:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
    BOOL drawTransposed = NO;


    return [self resizedImage:image
                          and:newSize
                    transform:[self transformForOrientation:newSize]
               drawTransposed:drawTransposed
         interpolationQuality:quality];
}


// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
// If the new size is not integral, it will be rounded up
- (UIImage *)resizedImage:(UIImage*) image and:(CGSize)newSize
                transform:(CGAffineTransform)transform
           drawTransposed:(BOOL)transpose
     interpolationQuality:(CGInterpolationQuality)quality {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
    CGImageRef imageRef = image.CGImage;

    // Build a context that's the same dimensions as the new size
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                CGImageGetBitmapInfo(imageRef));

    // Rotate and/or flip the image if required by its orientation
    CGContextConcatCTM(bitmap, transform);

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(bitmap, quality);

    // Draw into the context; this scales the image
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;
}



// Returns an affine transform that takes into account the image orientation when drawing a scaled image
- (CGAffineTransform)transformForOrientation:(CGSize)newSize {
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformTranslate(transform, newSize.width, 0);
    transform = CGAffineTransformScale(transform, -1, 1);
    return transform;
}

起初我认为这是由于内存不足引起的,但我已经对此进行了测试,这似乎不是问题,提前感谢他们

hi I am currently developing a small app on ios 4.3 , using objective c
as part of the app I need to manipulate an Image that I have downloaded from the web.
the following code shows up a missing image:

(the original is in a class but I just put this together as a test scenario so that it could be easily copy pasted)

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadImage:@"http://www.night-net.net/images/ms/microsoft_vista_home_basic.jpg"];
    [self getCroped:CGRectMake(10, 50, 80, 160)];
    [self getCroped:CGRectMake(90, 50, 80, 80)];
    [self getCroped:CGRectMake(90, 130, 40, 80)];
    [self getCroped:CGRectMake(130, 130, 40, 40)];
    [self getCroped:CGRectMake(130, 170, 40, 40)];

}
-(void) loadImage : (NSString*) url
{
     _data = [NSData dataWithContentsOfURL: 
              [NSURL URLWithString: url]];
}
-(UIImageView*) getCroped:(CGRect) imageSize{
    UIImage *temp = [[UIImage alloc] initWithData:_data];
    UIImage *myImage = [self resizedImage:temp and:CGSizeMake(160,160) interpolationQuality:kCGInterpolationHigh];
    UIImage *image = [self croppedImage:myImage and:imageSize];
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = image;
    imageView.frame = imageSize;
    [[self view] addSubview:imageView];

    return imageView;
}
- (UIImage *)croppedImage:(UIImage*) image and: (CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;
}


- (UIImage *)resizedImage:(UIImage*) image and:(CGSize)newSize interpolationQuality:(CGInterpolationQuality)quality {
    BOOL drawTransposed = NO;


    return [self resizedImage:image
                          and:newSize
                    transform:[self transformForOrientation:newSize]
               drawTransposed:drawTransposed
         interpolationQuality:quality];
}


// Returns a copy of the image that has been transformed using the given affine transform and scaled to the new size
// The new image's orientation will be UIImageOrientationUp, regardless of the current image's orientation
// If the new size is not integral, it will be rounded up
- (UIImage *)resizedImage:(UIImage*) image and:(CGSize)newSize
                transform:(CGAffineTransform)transform
           drawTransposed:(BOOL)transpose
     interpolationQuality:(CGInterpolationQuality)quality {
    CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
    CGRect transposedRect = CGRectMake(0, 0, newRect.size.height, newRect.size.width);
    CGImageRef imageRef = image.CGImage;

    // Build a context that's the same dimensions as the new size
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                newRect.size.width,
                                                newRect.size.height,
                                                CGImageGetBitsPerComponent(imageRef),
                                                0,
                                                CGImageGetColorSpace(imageRef),
                                                CGImageGetBitmapInfo(imageRef));

    // Rotate and/or flip the image if required by its orientation
    CGContextConcatCTM(bitmap, transform);

    // Set the quality level to use when rescaling
    CGContextSetInterpolationQuality(bitmap, quality);

    // Draw into the context; this scales the image
    CGContextDrawImage(bitmap, transpose ? transposedRect : newRect, imageRef);

    // Get the resized image from the context and a UIImage
    CGImageRef newImageRef = CGBitmapContextCreateImage(bitmap);
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];

    // Clean up
    CGContextRelease(bitmap);
    CGImageRelease(newImageRef);

    return newImage;
}



// Returns an affine transform that takes into account the image orientation when drawing a scaled image
- (CGAffineTransform)transformForOrientation:(CGSize)newSize {
    CGAffineTransform transform = CGAffineTransformIdentity;
    transform = CGAffineTransformTranslate(transform, newSize.width, 0);
    transform = CGAffineTransformScale(transform, -1, 1);
    return transform;
}

at first I thought this is caused by a lack of memory, but I have tested for that and that doesnt seem to be the problem,thanks in advance ofir

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2024-12-12 12:23:59

过去我遇到过问题,如果文件名中包含 unicode 字符,图像不会出现在 UIWebViews 中。我想知道这是否是同一件事。尝试重命名您的图像?

I've had issues in the past with images not appearing within UIWebViews if they contain unicode characters in the filename. I wonder if this might be the same thing. Try renaming your image?

花开浅夏 2024-12-12 12:23:59

这样做应该是可能的,并且内存成本很低,因为我做了同样的测试,使用闪存创建一个 iphone 应用程序,做同样的事情,并且它有效。
但我更喜欢使用 Objective C 所以问题仍然存在

doing this should be possible and low on memory cost as I did the same test,using flash to create an iphone app that does the same thing, and it works.
but I would much prefer using objective c so the question still stands

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文