为什么有些图像没有出现在iphone中

时间:2011-09-19 18:38:07

标签: iphone objective-c xcode uiimageview uiimage

您好我正在使用目标c在ios 4.3上开发一个小应用程序 作为应用程序的一部分,我需要操作我从网上下载的图像。 以下代码显示缺少的图像:

(原版是在一个类中,但我只是将它们放在一起作为测试场景,以便可以轻松地复制粘贴)

- (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;
}
起初我以为这是由缺乏记忆引起的,但是我已经测试了这个并且这似乎不是问题,感谢提前的

2 个答案:

答案 0 :(得分:0)

过去我遇到过一些问题,如果文件名中包含unicode字符,则UIWebViews中没有出现图像。我想知道这可能是同一件事。尝试重命名你的形象?

答案 1 :(得分:0)

这样做应该是可能的,并且内存成本很低,因为我做了同样的测试,使用flash来创建一个同样的东西的iPhone应用程序,它的工作原理。 但我更喜欢使用目标c所以问题仍然存在